简体   繁体   English

org.hibernate.AnnotationException: @OneToOne 或 @ManyToOne<entity> 引用一个未知实体

[英]org.hibernate.AnnotationException: @OneToOne or @ManyToOne on <entity> references an unknown entity

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

org.hibernate.AnnotationException: @OneToOne or @ManyToOne on cz.rohan.dusps.model.Switchport.konfiguracniTemplateAccess references an unknown entity: cz.rohan.dusps.model.KonfiguracniTemplate
    org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:103)
    org.hibernate.cfg.AnnotationConfiguration.processEndOfQueue(AnnotationConfiguration.java:541)
    org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:523)
    org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:380)
    org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1377)
    org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954)
    cz.rohan.dusps.helper.SessionFactoryHelper.initFactory(SessionFactoryHelper.java:122)
    cz.rohan.dusps.helper.SessionFactoryHelper.getSessionFactory(SessionFactoryHelper.java:134)
    cz.rohan.dusps.filter.HistorieZmenFilter.doFilter(HistorieZmenFilter.java:102)
    cz.rohan.dusps.filter.CharsetFilter.doFilter(CharsetFilter.java:41)

after ~20 hours spent on the problem with various people, having read every possible blog or forum, I am really getting desperate here.在与不同的人一起解决这个问题大约 20 个小时后,阅读了所有可能的博客或论坛,我真的很绝望。

This is a mid-sized project.这是一个中等规模的项目。 I should mention the database is Postgres 9.1 and we generate the DB using a modelling tool.我应该提到数据库是 Postgres 9.1,我们使用建模工具生成数据库。 Hibernate connects to the database but does not generate it. Hibernate 连接到数据库但不生成它。

I have created a new entity in the database, it's called "KonfiguracniTemplate" (configuration template).我在数据库中创建了一个新实体,名为“KonfiguracniTemplate”(配置模板)。 I have created the model, controller, form, validators, .jsp's, all basically copied 1:1 from an existing entity of a similar nature.我已经创建了模型、控制器、表单、验证器、.jsp,所有这些基本上都是从具有相似性质的现有实体中 1:1 复制的。 I can now work with KonfiguracniTemplate, CRUD is fully working.我现在可以使用 KonfiguracniTemplate,CRUD 完全正常工作。

The problem comes when I reference this KonfiguracniTemplate from the entity called Switchport.当我从名为 Switchport 的实体引用此 KonfiguracniTemplate 时,问题就出现了。 In the DB there is a relation between the two:在数据库中,两者之间存在关系:

  • Switchport 1:1 ... 0:N KonfiguracniTemplate (switchport always references a KonfiguracniTemplate; a KonfiguracniTemplate MAY BE referenced zero or more times) Switchport 1:1 ... 0:N KonfiguracniTemplate(switchport 总是引用一个 KonfiguracniTemplate;一个 KonfiguracniTemplate 可以被引用零次或多次)
  • Switchport has FK konfiguracniTemplateAccess_id for this relation. Switchport 具有此关系的 FK konfiguracniTemplateAccess_id。

In .../model/Switchport.java the relation is mapped just like all other relations that are working:在 .../model/Switchport.java 中,关系就像所有其他工作的关系一样被映射:

@ManyToOne
@JoinColumn(nullable = false)
private KonfiguracniTemplate konfiguracniTemplateAccess;

I have tried various forms:我尝试了各种形式:

@ManyToOne
@JoinColumn(name="konfiguracnitemplateaccess_id", nullable = false)
private KonfiguracniTemplate konfiguracniTemplateAccess;

or要么

@ManyToOne(targetEntity=KonfiguracniTemplate.class)
@JoinColumn(name="konfiguracnitemplateaccess_id", nullable = false)
private KonfiguracniTemplate konfiguracniTemplateAccess;

I have also checked:我也检查过:

  • both entities are in the same package两个实体都在同一个包中
  • they are both annotated "@Entity" using "import javax.persistence.Entity;"它们都使用“import javax.persistence.Entity;”注释为“@Entity”
  • the build produces no error/warning messages构建不会产生错误/警告消息
  • as long as the reference in Switchport is commented out, everything is fine只要把 Switchport 中的引用注释掉就没事

No matter what I try I cannot get rid of the "references an unknown entity" exception.无论我尝试什么,我都无法摆脱“引用未知实体”异常。 Can somebody please share an idea what is happening or maybe how to debug the issue?有人可以分享一下发生了什么或者如何调试问题吗? The stacktrace at the top of the post is all I get in the logs.帖子顶部的堆栈跟踪是我在日志中获得的全部信息。

All input is greatly appreciated!非常感谢所有输入!

只需将类Team添加到“ hibernate-cfg.xml ”文件中,因为 Hibernate 不添加就无法识别。

Possible Solutions:可能的解决方案:

1) Ensure that the entity has been appropriately referenced in hibernate.cfg.xml 1) 确保实体已在 hibernate.cfg.xml 中被适当引用

<hibernate-configuration>
<session-factory>
    ... 
    <mapping class="com.project.entitytwo.model.EntityTwo"/>
    ...
</session-factory>

2) Ensure that @Entity has been specified at the class-level ( at the top of the class ) 2)确保在类级别(在类的顶部)指定了@Entity

@Entity
@Table( name="ENTITY_TWO" )
public class EntityTwo extends AnyClass
{
    ...

I just had this problem, with entity a referencing entity b .我刚刚遇到了这个问题,实体a引用了实体b Both entities were located in a common JAR outside of the web project I was working on;两个实体都位于我正在处理的 Web 项目之外的一个公共 JAR 中; a was declared in persistence.xml but b wasn't. a在persistence.xml 中声明,但b没有。 I put b in a <class> tag in persistence.xml and it worked!我将b放在persistence.xml 中的<class>标记中,它起作用了!

I ran into this problem when using Spring and not using the hibernate.cfg.xml file.我在使用 Spring 而不是使用hibernate.cfg.xml文件时遇到了这个问题。 It was solved by adding the fully qualified package name of the Model class to the setPackagesToScan method of LocalSessionFactoryBean class.通过在setPackagesToScan类的setPackagesToScan方法中LocalSessionFactoryBean Model类的全限定包名来解决。

Finally got the solution from another developer on the team!终于从团队中的另一位开发人员那里得到了解决方案!

The classes need to be imported before the SessionFactory object is created.在创建 SessionFactory 对象之前需要导入这些类。 Here the import for the new class was missing, so it was unknown to the SessionFactory object.这里缺少新类的导入,因此 SessionFactory 对象不知道它。

Anyway, thanks everyone for your hints!不管怎样,谢谢大家的指点!

There is one more chance of getting such exception;还有一次机会得到这样的例外; when you don't mention your mapping class in hibernate.cfg.xml file.当您没有在 hibernate.cfg.xml 文件中提及您的映射类时。 As mentioned above.正如刚才提到的。

I had same exception... I just forget add annotation ( @Entity , and @Table ) on MASTER class(class with Primary key)我有同样的例外......我只是忘记在 MASTER 类(带有主键的类)上添加注释( @Entity@Table

so solution is double check every annotation in your entities , I mean not only @ManyToOne and @OneToMany like i did.所以解决方案是仔细检查实体中的每个注释,我的意思是不仅像我一样@ManyToOne@OneToMany

如果你的两个实体在不同的项目中,你可以在其他项目中扫描 KonfiguracniTemplate 的包。你可以在 spring boot 中这样做

@EntityScan({"com.thispackage.entity","com.KonfiguracniTemplatepackage.entity"})

I'll give you a solution that should work for the same error with Spring Boot .我会给你一个解决方案,它应该适用于Spring Boot的相同错误。 This has less to do with the original question, but today, people would probably look for this answer instead because noone uses XML configuration today anymore.这与最初的问题关系不大,但今天,人们可能会寻找这个答案,因为今天没有人再使用 XML 配置了。

I suffered the same problem and found the solution on this website: https://www.programmersought.com/article/1617314625/我遇到了同样的问题,在这个网站上找到了解决方案: https : //www.programmersought.com/article/1617314625/

He even describes this very question he would have looked up, but then I'm asking myself: why didn't he answered here after finding the solution?他甚至描述了他本来会查的这个问题,但后来我问自己:为什么他在找到解决方案后不在这里回答? LOL哈哈

His own words:他自己的话:

In the Spring Boot project, the default scan package is the package where the main method is located, that is, only the entity classes in the same package as the main method will be discovered .在Spring Boot项目中,默认的扫描包是main方法所在的包,即只有和main方法在同一个包中的实体类才会被发现 This way you can understand why User is not found: because User is an entity class in another module.这样你就可以理解为什么找不到 User 了:因为 User 是另一个模块中的实体类。 Spring Boot does not scan other packages at all; Spring Boot 根本不扫描其他包; Configure the @SpringBootApplication annotation on the main method that launches the application, telling Spring Boot that those packages need to be scanned: @SpringBootApplication(scanBasePackages = {“com.xiaomo.*”}) Then, User can be found.在启动应用程序的main方法上配置@SpringBootApplication注解,告诉Spring Boot需要扫描那些包: @SpringBootApplication(scanBasePackages = {“com.xiaomo.*”})然后就可以找到User了。

So you basically reconfigure SpringBoot to scan more packages to include the other ones.因此,您基本上重新配置 SpringBoot 以扫描更多包以包含其他包。

My personal addition: you could also move your packages into the package where the starter is located or move the starter a package up (that's what I did).我个人的补充:您还可以将您的包裹移动到启动器所在的包裹中或将启动器向上移动一个包裹(这就是我所做的)。

暂无
暂无

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

相关问题 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 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 org.hibernate.AnnotationException:@OneToOne on com.abc.dto.Test.name 引用了一个未知实体:com.abc.type.TestName - org.hibernate.AnnotationException:@OneToOne on com.abc.dto.Test.name references an unknown entity: com.abc.type.TestName 引起:org.hibernate.AnnotationException: @OneToOne 或 @ManyToOne on - Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on org.hibernate.AnnotationException: @OneToOne 或 @ManyToOne - org.hibernate.AnnotationException: @OneToOne or @ManyToOne AnnotationException:org.am.Car.engine上的@OneToOne或@ManyToOne引用了未知实体:org.am.Engine - AnnotationException: @OneToOne or @ManyToOne on org.am.Car.engine references an unknown entity: org.am.Engine 获取org.hibernate.AnnotationException:未知实体名称:int - Getting org.hibernate.AnnotationException: Unknown entity name: int org.hibernate.AnnotationException:mappedBy通过引用未知目标实体属性 - org.hibernate.AnnotationException: mappedBy reference an unknown target entity property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM