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

前端 未结 7 836
梦谈多话
梦谈多话 2020-11-28 04:52

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 Li

相关标签:
7条回答
  • 2020-11-28 05:08

    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());
    
    0 讨论(0)
  • 2020-11-28 05:10

    Your entity may not listed in hibernate configuration file.

    0 讨论(0)
  • 2020-11-28 05:11

    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.

    0 讨论(0)
  • 2020-11-28 05:16

    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;
    }
    
    0 讨论(0)
  • 2020-11-28 05:16

    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>
    
    0 讨论(0)
  • 2020-11-28 05:17

    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 {
    }
    
    0 讨论(0)
提交回复
热议问题