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

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!

This question is related to hibernate jpa

The answer is


If you are using Java configuration in a spring-data-jpa project, make sure you are scanning the package that the entity is in. For example, if the entity lived com.foo.myservice.things then the following configuration annotation below would not pick it up.

You could fix it by loosening it up to just com.foo.myservice (of course, keep in mind any other effects of broadening your scope to scan for entities).

@Configuration
@EnableJpaAuditing
@EnableJpaRepositories("com.foo.myservice.repositories")
public class RepositoryConfiguration {
}

Mostly in Hibernate , need to add the Entity class in hibernate.cfg.xml like-

<hibernate-configuration>
  <session-factory>

    ....
    <mapping class="xxx.xxx.yourEntityName"/>
 </session-factory>
</hibernate-configuration>

Your entity may not listed in hibernate configuration file.


I had the same problem and I could solve it by adding the entity into persistence.xml. The problem was caused due to the fact that the entity was not added to the persistence config. Edit your persistence file:

<persistence-unit name="MY_PU" transaction-type="RESOURCE_LOCAL">
<provider>`enter code here`
     org.hibernate.jpa.HibernatePersistenceProvider
  </provider>
<class>mypackage.MyEntity</class>

...


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;
}

In my case a has to add my classes, when building the SessionFactory, with addAnnotationClass

Configuration configuration.configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
SessionFactory sessionFactory = configuration
            .addAnnotatedClass(MyEntity1.class)
            .addAnnotatedClass(MyEntity2.class)
            .buildSessionFactory(builder.build());