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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3983135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 07:54:29  来源:igfitidea点击:

Hibernate @ManyToOne references an unknown entity

javahibernatejpa

提问by user482368

I am receiving the following Hibernate Exception:

我收到以下休眠异常:

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

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

Can anybody shed light on why this exception is occurring?

任何人都可以阐明为什么会发生这种异常吗?

采纳答案by user482368

I figured out the problem: I was not adding class Team to the Hibernate AnnotationConfigurationobject. Thus, Hibernate was not recognizing the class.

我找到了问题所在:我没有将 Team 类添加到 HibernateAnnotationConfiguration对象中。因此,Hibernate 无法识别该类。

回答by TomKim

Another solution: Check to ensure that the referenced class is included your hibernate.cfg.xmlfile.

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

回答by Summer

Along with entry in hibernate.cfg.xml, you'll need @Entityannotation on referenced class.

随着 hibernate.cfg.xml 中的条目,您需要@Entity对引用的类进行注释。

回答by user3487222

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

回答by Monkey Supersonic

I work in a project using Spring and Hibernate 4 and I found out that we do nothave a hibernate.cfg.xmlfile. Instead, our beans are listed in the file applicationContext.xmlwhich looks a bit like

我在使用Spring和Hibernate 4的一个项目工作,我发现,我们并没有有一个hibernate.cfg.xml文件。相反,我们的 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. You can find some more information here.

将我的 bean 添加到列表中解决了这个问题。您可以在此处找到更多信息。

回答by manjari.rastogi

Add the class in hibernate.cfgin proper order. First map the file that is going to be referred by another class

hibernate.cfg按正确的顺序添加类。首先映射将要被另一个类引用的文件

回答by Yogesh Kumar Gupta

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. The package was in upper case(DAO) and the packageToscan had dao as its value. just wanted to add this in case some one find it help full

我遇到了同样的问题,从过去几个小时开始我就一直在努力解决这个问题。我终于发现 packageToscan 属性中的值和实际包名称有大小写不匹配。包是大写的(DAO),packageToscan 的值是 dao。只是想添加这个以防万一有人发现它有帮助

回答by Den Dee

If you do not use the hibernate.cfg.xmlyou can add targetEntity parameter in @ManyToOne/@OneToManyannotation with class describing your entity.

如果您不使用 ,则hibernate.cfg.xml可以在@ManyToOne/@OneToMany带有描述实体的类的注释中添加 targetEntity 参数。

For instance:

例如:

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