Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 冬眠问题-“;针对未映射类使用@OneToMany或@ManyToMany“;_Hibernate_Jpa - Fatal编程技术网

Hibernate 冬眠问题-“;针对未映射类使用@OneToMany或@ManyToMany“;

Hibernate 冬眠问题-“;针对未映射类使用@OneToMany或@ManyToMany“;,hibernate,jpa,Hibernate,Jpa,我在Hibernate注释中找到了自己的脚,我遇到了一个问题,希望有人能帮我解决 我有两个实体,部分和范围主题。该节有一个列表类成员,因此是一对多关系。当我运行单元测试时,我遇到以下异常: 使用@OneToMany或@ManyToMany以未映射的类为目标:com.xxx.domain.Section.scopeTopic[com.xxx.domain.scopeTopic] 我假设这个错误意味着我的ScopeTopic实体没有映射到表?我看不出我做错了什么。以下是实体类: 我很确定这是我自

我在Hibernate注释中找到了自己的脚,我遇到了一个问题,希望有人能帮我解决

我有两个实体,部分和范围主题。该节有一个列表类成员,因此是一对多关系。当我运行单元测试时,我遇到以下异常:

使用@OneToMany或@ManyToMany以未映射的类为目标:com.xxx.domain.Section.scopeTopic[com.xxx.domain.scopeTopic]

我假设这个错误意味着我的ScopeTopic实体没有映射到表?我看不出我做错了什么。以下是实体类:




我很确定这是我自己的理解不足造成的,所以一些指导会很好,谢谢

您的注释看起来不错。以下是需要检查的事项:

  • 确保注释是
    javax.persistence.Entity
    ,而不是
    org.hibernate.annotations.Entity
    。前者使实体可检测。后者只是一个补充

  • 如果要手动列出实体(在persistence.xml、hibernate.cfg.xml或配置会话工厂时),请确保还列出了
    ScopeTopic
    实体

  • 确保在不同的包中没有多个
    ScopeTopic
    类,并且导入了错误的类


您的实体可能未列在hibernate配置文件中。

我的实体在多方实体上没有
@entity

@Entity // this was commented
@Table(name = "some_table")
public class ChildEntity {
    @JoinColumn(name = "parent", referencedColumnName = "id")
    @ManyToOne
    private ParentEntity parentEntity;
}

主要在
Hibernate
中,需要在
Hibernate.cfg.xml
中添加
实体
类,如-

<hibernate-configuration>
  <session-factory>

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

....

我也遇到了同样的问题,我可以通过将实体添加到persistence.xml中来解决它。 该问题是由于实体未添加到持久性配置而导致的。编辑您的持久性文件:

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

`在这里输入代码`
org.hibernate.jpa.HibernatePersistenceProvider
mypackage.MyEntity

在我的例子中,当使用
addAnnotationClass
构建
会话工厂时,a必须添加我的类

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

如果您在SpringDataJPA项目中使用Java配置,请确保您正在扫描实体所在的包。例如,如果实体存在com.foo.myservice.things,那么下面的配置注释将不会拾取它

您可以通过将其放宽到com.foo.myservice来修复它(当然,请记住扩大范围以扫描实体的任何其他影响)


检查@Entity类的包结构

我还遇到了一个类似的问题,因为我意外拼错了包名

确保所有@Entity类都位于具有main()函数的类的包内或下的包(文件夹)下,因为SpringBootApplication将只扫描同一个包内的包

如果您的@Entity类未被扫描,它将不会被映射,并将给出所述错误


您还可以通过检查数据库来检查@Entity类是否正确映射或扫描。每当一个类被标记为@Entity时,Hibernate/JPA就会在相应的数据库中自动生成一个表。

Ah,谢谢!第2点是关键,在创建SessionFactory时,我忘记将ScopeTopic放在AnnotatedClass属性列表中,n00b错误!对于那些刚刚发表评论的人。在hibernate 4中不推荐使用org.hibernate.annotations.Entity。第1点不再适用。对我来说是第2点,因为我一直在编辑构建文件夹中的hibernate.cfg.xml。在Spring Boot中,我遇到了一个问题,第2点帮助我解决了这个问题。我一直让Spring Boot自动配置我的会话工厂,在一个分支中我添加了一个新实体。。。然后在合并之前,我有另一个分支,在那里我必须自定义会话工厂。两个分支同时被合并,我得到了这个问题中列出的错误。spring boot不再自动扫描我的新实体,因为我已经配置了sessionfactory,并且没有考虑包含来自其他分支的新实体。在我的情况下,我忘记了使用
packagesToScan
<persistence-unit name="MY_PU" transaction-type="RESOURCE_LOCAL">
<provider>`enter code here`
     org.hibernate.jpa.HibernatePersistenceProvider
  </provider>
<class>mypackage.MyEntity</class>
Configuration configuration.configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
SessionFactory sessionFactory = configuration
            .addAnnotatedClass(MyEntity1.class)
            .addAnnotatedClass(MyEntity2.class)
            .buildSessionFactory(builder.build());
@Configuration
@EnableJpaAuditing
@EnableJpaRepositories("com.foo.myservice.repositories")
public class RepositoryConfiguration {
}