简体   繁体   English

Hibernate @ManyToOne 引用了一个未知实体

[英]Hibernate @ManyToOne references an unknown entity

I am receiving the following Hibernate Exception:我收到以下休眠异常:

@OneToOne or @ManyToOne on Matchup.awayTeam references an unknown entity: Team

The simplified Matchup class looks like this:简化的 Matchup 类如下所示:

@Entity public class Matchup implements Serializable 
{
   protected Team awayTeam;

   @ManyToOne 
   @JoinColumn(name="away_team_id")
   public Team getAwayTeam() {
      return awayTeam;
   }
}

The simplified Team class looks like this:简化的 Team 类如下所示:

@Entity
public class Team implements Serializable {
    protected List<Matchup> matchups;

    @OneToMany(mappedBy="awayTeam", targetEntity = Matchup.class,
    fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    public List<Matchup> getMatchups() {
       return matchups;
    }
}

Notes:笔记:

  • Both Matchup and Team have subclasses. Matchup 和 Team 都有子类。 I'm not sure if this impacts the situation.我不确定这是否会影响情况。
  • Both Matchup and Team are listed in my persistence.xml as being included. Matchup 和 Team 都列在我的persistence.xml 中作为包含。
  • If I put @Transient annotations on both getter methods, the error disappears.如果我将 @Transient 注释放在两个 getter 方法上,错误就会消失。

Can anybody shed light on why this exception is occurring?任何人都可以阐明为什么会发生这种异常吗?

I figured out the problem: I was not adding class Team to the Hibernate AnnotationConfiguration object.我发现了问题:我没有将类 Team 添加到 Hibernate AnnotationConfiguration对象中。 Thus, Hibernate was not recognizing the class.因此,Hibernate 无法识别该类。

除了 hibernate.cfg.xml 中的条目外,您还需要在引用的类上添加@Entity注释。

另一个解决方案:检查以确保引用的类包含在您的hibernate.cfg.xml文件中。

If you are using Spring Boot in combination with hibernate, then actually you may just need to add the package to your @EntityScan base package array.如果您将 Spring Boot 与 hibernate 结合使用,那么实际上您可能只需要将该包添加到您的@EntityScan基本包数组中。

@SpringBootApplication(scanBasePackages = {"com.domain.foo.bar.*"})
@EnableJpaRepositories(basePackages ={"com.domain.foo.bar.*"})
@EntityScan(basePackages ={"com.domain.foo.bar.*", "com.domain.another.*"})
public class SpringBootApplication extends SpringBootServletInitializer {
}

I work in a project using Spring and Hibernate 4 and I found out that we do not have a hibernate.cfg.xml file.我在一个使用 Spring 和 Hibernate 4 的项目中工作,我发现我们没有hibernate.cfg.xml文件。 Instead, our beans are listed in the file applicationContext.xml which looks a bit like相反,我们的 bean 列在文件applicationContext.xml ,看起来有点像

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
    <property name="annotatedClasses">
        <list>
            <value>com.package.Bean</value>
        </list>
    </property>
</bean>

Adding my bean to the list solved the problem.将我的 bean 添加到列表中解决了这个问题。 You can find some more information here .您可以在此处找到更多信息。

If you do not use the hibernate.cfg.xml you can add targetEntity parameter in @ManyToOne/@OneToMany annotation with class describing your entity.如果您不使用hibernate.cfg.xml您可以在@ManyToOne/@OneToMany注释中添加 targetEntity 参数以及描述您的实体的类。

For instance:例如:

@ManyToOne(targetEntity = some.package.MyEntity.class)

就我而言,它是persistence.xml ,它列出了除一个以外的所有实体类...

Try to add the Qualified Name (ClassNAME), just like this:尝试添加 Qualified Name (ClassNAME),就像这样:

<hibernate-configuration>
    <session-factory name="java:/hibernate/SessionFactory">
                  <mapping class="co.com.paq.ClassNAME" />
        </session-factory>
</hibernate-configuration>

In the File:在文件中:

META-INF/hibernate.cfg.xml

Add the class in hibernate.cfg in proper order.按照正确的顺序在hibernate.cfg中添加类。 First map the file that is going to be referred by another class首先映射将要被另一个类引用的文件

I had the same problem and I was struggling with it from last couple of hours.我遇到了同样的问题,从过去几个小时开始我就一直在努力解决这个问题。 I finally found out that the value in packageToscan property and the actual package name had case mismatch.我终于发现 packageToscan 属性中的值和实际包名称有大小写不匹配。 The package was in upper case(DAO) and the packageToscan had dao as its value.包是大写的(DAO),packageToscan 的值是 dao。 just wanted to add this in case some one find it help full只是想添加这个以防万一有人发现它有帮助

Add the Entity in all persistence-units of your persistence.xmlpersistence.xml 的所有持久性单元中添加实体

Example例子

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence              http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="SystemDataBaseDS" transaction-type="JTA">
    <jta-data-source>java:jboss/datasources/SystemDataBaseDS</jta-data-source>
    <class>package.EntityClass</class>
    ...
  </persistence-unit>
  <persistence-unit name="SystemDataBaseJDBC" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>package.EntityClass</class>
    ...
  </persistence-unit>
</persistence>

In my case I had to look through all classes annotated with @Configuration .就我而言,我必须查看所有用@Configuration注释的类。 One of the classes defined an EntityManagerFactory bean as follows:其中一个类定义了一个EntityManagerFactory bean,如下所示:

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());
    em.setPackagesToScan("com.mypackage.entities");
    // omitted
}

whereas I put a new entity in the package com.mypackage.components.user.persistence .而我在包com.mypackage.components.user.persistence放置了一个新实体。 So I had to update the last line of the EntityManagerFactory bean definition to:所以我不得不将EntityManagerFactory bean 定义的最后一行更新为:

em.setPackagesToScan("com.mypackage.entities", "com.mypackage.components");

Also, it won't hurt to look at the @EnableJpaRepositories annotation and check its basePackages attribute as well.此外,查看@EnableJpaRepositories注释并检查其basePackages属性也basePackages It might well be the case that your new entity resides outside the packages listed in the attribute's value.很可能您的新实体位于属性值中列出的包之外。

我有这个问题,因为我没有将addAnnotaitedClass(SomeClass.class)SessionFactory factory = new Configuration().configure("hibernate.cfg.xml"). “SomeClass”类SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").

The unknown entity error is not a hibernate annotation problem, but rather that the entity is NOT being recognized. unknown entity错误不是休眠注释问题,而是实体未被识别。 You should check your persistence settings, whether you are using pure JPA, Hibernate or Spring.您应该检查您的持久性设置,无论您使用的是纯 JPA、Hibernate 还是 Spring。

By default, Hibernate is capable of finding the JPA entity classes based on the presence of the @Entity annotation, so you don't need to declare the entity classes.默认情况下,Hibernate 能够根据 @Entity 注释的存在来查找 JPA 实体类,因此您不需要声明实体类。

With Spring you would have an @Bean similar to the following to make sure you don't have unknown entities in your package.使用 Spring,您将拥有一个类似于以下内容的@Bean ,以确保您的包中没有未知实体。

@Bean
open fun entityManagerFactory() : 
LocalContainerEntityManagerFactoryBean {
    val em = LocalContainerEntityManagerFactoryBean()
    em.setPackagesToScan("com.package.domain.entity")

    //...
}

GL GL

Source来源

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hibernate“ManyToOne ...引用未知实体”异常 - Hibernate “ManyToOne … references an unknown entity” exception org.hibernate.AnnotationException: @OneToOne 或 @ManyToOne<entity> 引用一个未知实体 - org.hibernate.AnnotationException: @OneToOne or @ManyToOne on <entity> references an unknown entity @OneToOne 或 @ManyToOne 引用未知实体 - @OneToOne or @ManyToOne on references an unknown entity Java Hibernate org.hibernate.AnnotationException:@OneToOne或@ManyToOne <class> 引用未知实体: <class> - Java Hibernate org.hibernate.AnnotationException: @OneToOne or @ManyToOne on <class> references an unknown entity: <class> 由以下原因引起:org.hibernate.AnnotationException:XX上的@OneToOne或@ManyToOne引用了未知实体:YY - Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on XX references an unknown entity: YY com.hibernate.domain.Bee.honey上的@OneToOne或@ManyToOne引用未知实体 - @OneToOne or @ManyToOne on com.hibernate.domain.Bee.honey references an unknown entity org.hibernate.AnnotationException:entities.Ques#tion.examId 上的 @OneToOne 或 @ManyToOne 引用了一个未知实体:long - org.hibernate.AnnotationException: @OneToOne or @ManyToOne on entities.Ques#tion.examId references an unknown entity: long 不断获取 org.hibernate.AnnotationException: @OneToOne 或 @ManyToOne 引用未知实体:错误 - Constantly getting org.hibernate.AnnotationException: @OneToOne or @ManyToOne references an unknown entity: error JPA OneToMany ManyToOne @OneToOne或@ManyToOne在引用未知实体时: - JPA OneToMany ManyToOne @OneToOne or @ManyToOne on references an unknown entity: Spring 引导错误:@OneToOne 或 @ManyToOne 引用未知实体 - Spring Boot Error: @OneToOne or @ManyToOne references an unknown entity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM