2

I need help to create the correct pojo's from this database...

https://www.dropbox.com/s/j2lfu44zpqfcxb4/dbr.PNG

I have tried creating this classes...

 @Entity @Table(name="Municipio", catalog="elecciones2014", schema="") public class Municipio implements Serializable{ @EmbeddedId private MunicipioPk idMunicipio; @Basic(optional=false) @Column(name="nomb_municipio") private String nomb_municipio; } 

With this Embedded class

 @Embeddable class MunicipioPk implements Serializable{ /** * */ private static final long serialVersionUID = 1L; @Column(name="id_depto") String departamento; @Column(name="id_municipio") String idMunicipio; } 

The problem is when i want to reference to 'Municipio' from 'JRV' y don't know how to access to field 'id_municipio'. I had this code but it doesn't work

 @Entity @Table(name = "JRV", catalog = "elecciones2014", schema = "") public class Jrv { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(optional = false) @Column(name = "id_jrv") private int id; @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="id_municipio",referencedColumnName="idMunicipio") private Municipio municipio; @ManyToOne @JoinColumn(name="DUI",referencedColumnName="dui") private PadronElectoral dui; } 

can someone help me? how I have to do it? Thanks in advice!!

1 Answer 1

1

Here you are defining single join column, but the Municipio entity's PK has two columns. Also the referencedColumnName should be the name of the column not the entity's property.

@ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="id_municipio",referencedColumnName="idMunicipio") private Municipio municipio; 

So you could do something like this:

@ManyToOne(fetch=FetchType.LAZY) @JoinColumns({ @JoinColumn(name="id_municipio", referencedColumnName="id_municipio"), @JoinColumn(name="id_depto", referencedColumnName="id_depto") }) private Municipio municipio; 

Which translates to this SQL (I got this by generating SQL schema from your entities after the modification mentioned above):

create table elecciones2014.JRV ( id_jrv serial not null, id_depto varchar(255), id_municipio varchar(255), primary key (id_jrv) ); alter table elecciones2014.JRV add constraint FK_7scd8alu3nf4tsyh3hq2ryrja foreign key (id_depto, id_municipio) references elecciones2014.Municipio; 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your help, but now i get this exception org.hibernate.MappingException: Unable to find column with logical name: id_municipio in org.hibernate.mapping.Table(elecciones2014.Municipio) and its related supertables and secondary tables
Strange ... it worked for me when I tried it on the entities in your question. Are your entities the same in your project as in your question or have you modified them somehow in the meantime? Specifically check if your municipio key in your embedded ID is really id_municipio. Otherwise I don't see a reason why this wouldn't work.
You were right, it worked perfectly. I had an error in my project's code, but after solve it your answer worked. Thanks a lot!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.