Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate problem - "Use of @OneToMany or @ManyToMany targeting an unmapped class"

Tags:

hibernate

jpa

I'm finding my feet with Hibernate Annotations and I've hit a problem I hope someone can help with.

I have 2 entities, Section and ScopeTopic. The section has a List class member, so a One to Many relationship. When I run my unit test I am getting this exception:

Use of @OneToMany or @ManyToMany targeting an unmapped class: com.xxx.domain.Section.scopeTopic[com.xxx.domain.ScopeTopic]

I would assume that the error implies that my ScopeTopic entity isn't mapped to a table? I can't see with I have done wrong. Here are the Entity classes:


@Entity public class Section {     private Long id;     private List<ScopeTopic> scopeTopics;      public Section() {}      @Id     public Long getId() {         return id;     }      public void setId(Long id) {         this.id = id;     }      @OneToMany     @JoinTable(name = "section_scope", joinColumns = {@JoinColumn(name="section_id")},                inverseJoinColumns = {@JoinColumn(name="scope_topic_id")} )     public List<ScopeTopic> getScopeTopic() {         return scopeTopic;     }      public void setScopeTopic(List<ScopeTopic> scopeTopic) {         this.scopeTopic = scopeTopic;     } } 

@Entity @Table(name = "scope_topic") public class ScopeTopic {     private Long id;     private String topic;      public ScopeTopic() {}      @Id     public Long getId() {         return id;     }      public void setId() {         this.id = id;     }      public String getTopic() {         return topic;     }      public void setTopic(String topic) {         this.topic = topic;     } } 

I'm pretty sure it's my own lack of understanding that's at fault so some guidance would be great, thanks!

like image 528
C0deAttack Avatar asked Feb 10 '11 12:02

C0deAttack


2 Answers

Your annotations look fine. Here are the things to check:

  • make sure the annotation is javax.persistence.Entity, and not org.hibernate.annotations.Entity. The former makes the entity detectable. The latter is just an addition.

  • if you are manually listing your entities (in persistence.xml, in hibernate.cfg.xml, or when configuring your session factory), then make sure you have also listed the ScopeTopic entity

  • make sure you don't have multiple ScopeTopic classes in different packages, and you've imported the wrong one.

like image 121
Bozho Avatar answered Sep 18 '22 15:09

Bozho


Mine was not having @Entity on the many side entity

@Entity // this was commented @Table(name = "some_table") public class ChildEntity {     @JoinColumn(name = "parent", referencedColumnName = "id")     @ManyToOne     private ParentEntity parentEntity; } 
like image 32
Youans Avatar answered Sep 16 '22 15:09

Youans