2
Hello, I have a relationship @ManyToMany
bidirectional between the class Pessoa
and Endereço
, where for these, I have the tables in the bank for person, address and the table for the relationship between them, the rel_pes_endereco
.
It happens that when I query the HQL to test the relationship of the classes, selecting the class Person shows the following error.
org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: model.Endereco.pessoas[model.Pessoa]
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1134)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:793)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:728)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1695)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
Classes have been declared in cfg.xml
<mapping class="model.Endereco"/>
<mapping class="model.Pessoa"/>
The annotations are as follows:
@Entity
@Table(name = "tab_pessoa")
public class Pessoa implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id_pes")
private int id;
@ManyToMany(targetEntity = Endereco.class, fetch = FetchType.LAZY)
@JoinTable(name = "rel_pes_endereco",
joinColumns = { @JoinColumn(name = "id_pes") },
inverseJoinColumns = { @JoinColumn(name = "id_endereco") } )
private List<Endereco> enderecos;
//Get Set
}
@Entity
@Table(name = "tab_endereco")
public class Endereco implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id_endereco")
private int id;
@ManyToMany(mappedBy = "enderecos", targetEntity = Pessoa.class)
private List<Pessoa> pessoas;
//Get e Set
}
I started using the tool recently, I looked at some related problems and documentation and was creating as, I made this same mapping in several other ways, but always get the same message.
Could someone help?
Thanks in advance.
What package are you using @Entity from? See if it’s in the javax.persistence.Entity package
– Edjane
Yes, in both classes I’m only using the javax.persistence package.
– Lucas
Have you declared hashcode(), equals() and toString() in the Person and Address classes? Do a test in List<Person> people, only with mappedBy = "addresses" and see what happens
– Edjane
I just hadn’t put toString(). I left in the List<Person> class "Address" only with @Manytomany(mappedBy = "addresses"), but the error persisted.
– Lucas
One more attempt, rs... instance people as follows: private List<Person> people = new Arraylist<Person>();
– Edjane
I put the Arraylist<Person>(), and then tried with Arraylist<Address>() too, but the error continued the same, tense kk
– Lucas
Really tense, rs. You urged the two variables, right? I noticed one more thing, you’re mapping class="model.Address", your folder structure this java -> model? If that’s not the case I would try to use instead of @Manytomany(targetEntity = Endereco.class, fetch = Fetchtype.LAZY) like @Manytomany(Cascade = Cascadetype.ALL).
– Edjane
@Lucasnegrerio How you are using annotations in your classes, the file
cfg.xml
is not necessary. Remove this file from your project and try running the application again to see if the error persists. Also, which version of Hibernate you are using?– Felipe Marinho
@Felipemarinho I’m using version 4.3.x. I did the test, took cfg.xml, but started pointing out errors in other parts of the project. I believe Hibernate uses the settings of this file at start.
– Lucas
@Lucasnegrerio You said the exception occurs when you use HQL , maybe the problem is in it. Edit your question and add it.
– Felipe Marinho
Hello @Felipemarinho, I did a test outside of HQL, I created a page and a bean and when I try to instantiate the Person or Address classes, or any of the classes involved with these, it also presents the same error message as in HQL. I no longer know how to test or what to verify, JPA imports, the bank table, mapping, only thing I did not do, was create a model to make their relationship, as well as Manytomany’s relational entity, but I do not think feasible with this tool.
– Lucas
@Lucasnegrerio I did a test with the classes you put here (without the cfg.xml file) and it worked normally. I was able to insert and retrieve the entities from the bank. What you can do is create a new project and add the classes gradually and go testing to try to find the error. If possible, test with a newer version of Hibernate. Also, check if the class
Pessoa
andEndereco
are in the model package.– Felipe Marinho
The problem with Manytomany must be in the HQL of cfg.xml that was using the same test. Also changed in the bank, in the table that makes the Join das entitys, one of the fields Id was as Unique. I came to create a new project and remade, HQL continued pointing the error, but in the page I made as a test, made the Serts in the right bank. Thanks to you guys!
– Lucas
Finally, in the annotations the only thing I changed was the new Arraylist<> I added, because to take the addresses and set a new one in the person was occurring error. private List<Address> addresses = new Arraylist<Address>(); >();
– Lucas