org.hibernate.AnnotationException:上的 @OneToOne 或 @ManyToOne引用未知实体

发布于 2024-12-04 03:39:35 字数 2563 浏览 1 评论 0原文

我收到以下 Hibernate 异常:

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)

在与不同的人一起解决这个问题大约 20 个小时之后,阅读了所有可能的博客或论坛,我真的在这里感到绝望。

这是一个中型项目。我应该提到数据库是 Postgres 9.1,我们使用建模工具生成数据库。 Hibernate 连接到数据库但不生成它。

我在数据库中创建了一个新实体,称为“KonfiguracniTemplate”(配置模板)。我已经创建了模型、控制器、表单、验证器、.jsp,所有这些基本上都是从具有类似性质的现有实体 1:1 复制的。我现在可以使用 KonfiguracniTemplate,CRUD 完全可以工作。

当我从名为 Switchport 的实体引用此 KonfiguracniTemplate 时,问题就出现了。在数据库中,两者之间存在关系:

  • Switchport 1:1 ... 0:N KonfiguracniTemplate(switchport 始终引用 KonfiguracniTemplate;KonfiguracniTemplate 可以被引用零次或多次)
  • Switchport 具有用于此关系的 FK konfiguracniTemplateAccess_id。

在 .../model/Switchport.java 中,关系的映射就像所有其他正在工作的关系一样:

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

我尝试了各种形式:

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

或者

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

我也检查过:

  • 两个实体都在同一个包中,
  • 它们都注释为“@Entity”使用“导入javax.persistence.Entity;”
  • 构建就不会产生错误/警告消息
  • 只要 Switchport 中的引用被注释掉,

,一切都很好无论我尝试什么,我都无法摆脱“引用未知实体”异常。有人可以分享一下发生了什么或者如何调试问题吗?帖子顶部的堆栈跟踪是我在日志中得到的全部内容。

非常感谢所有的输入!

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.

This is a mid-sized project. I should mention the database is Postgres 9.1 and we generate the DB using a modelling tool. Hibernate connects to the database but does not generate it.

I have created a new entity in the database, it's called "KonfiguracniTemplate" (configuration template). I have created the model, controller, form, validators, .jsp's, all basically copied 1:1 from an existing entity of a similar nature. I can now work with KonfiguracniTemplate, CRUD is fully working.

The problem comes when I reference this KonfiguracniTemplate from the entity called Switchport. 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 has FK konfiguracniTemplateAccess_id for this relation.

In .../model/Switchport.java the relation is mapped just like all other relations that are working:

@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;"
  • the build produces no error/warning messages
  • as long as the reference in Switchport is commented out, everything is fine

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

懵少女 2024-12-11 03:39:35

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

Just add the class Team to the "hibernate-cfg.xml" file, because Hibernate doesn't identify without adding into it.

心清如水 2024-12-11 03:39:35

可能的解决方案:

1) 确保在 hibernate.cfg.xml 中正确引用了该实体

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

2) 确保已在类级别(在类的顶部)指定了 @Entity

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

Possible Solutions:

1) Ensure that the entity has been appropriately referenced in 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 )

@Entity
@Table( name="ENTITY_TWO" )
public class EntityTwo extends AnyClass
{
    ...
窝囊感情。 2024-12-11 03:39:35

我刚刚遇到这个问题,实体 a 引用实体 b。这两个实体都位于我正在开发的 Web 项目之外的一个公共 JAR 中; a 已在 persistence.xml 中声明,但 b 未声明。我将 b 放入 persistence.xml 的 标记中,它成功了!

I just had this problem, with entity a referencing entity b. Both entities were located in a common JAR outside of the web project I was working on; a was declared in persistence.xml but b wasn't. I put b in a <class> tag in persistence.xml and it worked!

风铃鹿 2024-12-11 03:39:35

我在使用 Spring 而没有使用 hibernate.cfg.xml 文件时遇到了这个问题。通过将 Model 类的完全限定包名称添加到 LocalSessionFactoryBean 类的 setPackagesToScan 方法中解决了这个问题。

I ran into this problem when using Spring and not using the hibernate.cfg.xml file. It was solved by adding the fully qualified package name of the Model class to the setPackagesToScan method of LocalSessionFactoryBean class.

破晓 2024-12-11 03:39:35

终于从团队中的另一位开发人员那里得到了解决方案!

需要在创建 SessionFactory 对象之前导入这些类。这里缺少新类的导入,因此 SessionFactory 对象不知道它。

不管怎样,谢谢大家的提示!

Finally got the solution from another developer on the team!

The classes need to be imported before the SessionFactory object is created. Here the import for the new class was missing, so it was unknown to the SessionFactory object.

Anyway, thanks everyone for your hints!

小苏打饼 2024-12-11 03:39:35

还有一次获得此类例外的机会;当您没有在 hibernate.cfg.xml 文件中提及您的映射类时。
如上所述。

There is one more chance of getting such exception; when you don't mention your mapping class in hibernate.cfg.xml file.
As mentioned above.

忱杏 2024-12-11 03:39:35

我有同样的例外...我只是忘记在 MASTER 类(带有主键的类)上添加注释(@Entity@Table),

所以解决方案是仔细检查每个注释在您的实体中,我的意思不仅仅是像我一样的 @ManyToOne@OneToMany

I had same exception... I just forget add annotation (@Entity, and @Table) on MASTER class(class with Primary key)

so solution is double check every annotation in your entities , I mean not only @ManyToOne and @OneToMany like i did.

断念 2024-12-11 03:39:35

如果您的两个实体位于不同的项目中,您可以扫描其他项目中的 KonfiguracniTemplate 的包。您可以在 Spring Boot 中这样做


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

if your two entity in diffrent project,you can scan KonfiguracniTemplate's package in other project.you can do like this in spring boot


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

嘦怹 2024-12-11 03:39:35

我将为您提供一个解决方案,该解决方案应该适用于 Spring Boot 的相同错误。这与最初的问题关系不大,但今天,人们可能会寻找这个答案,因为现在没有人再使用 XML 配置了。

我遇到了同样的问题,并在这个网站上找到了解决方案: https://www.programmersought.com/ Article/1617314625/

他甚至描述了他本来会查找的这个问题,但后来我问自己:为什么他在找到解决方案后不在这里回答?哈哈

他自己的话:

在Spring Boot项目中,默认扫描包是main方法所在的包,即只有与main方法同包的实体类才会被扫描发现。这样就可以理解为什么找不到User了:因为User是另一个模块中的实体类。 Spring Boot 根本不扫描其他包;
在启动应用程序的 main 方法上配置 @SpringBootApplication 注解,告诉 Spring Boot 需要扫描这些包: @SpringBootApplication(scanBasePackages = {“com.xiaomo.*”})
然后就可以找到User了。


因此,您基本上重新配置 SpringBoot 以扫描更多包以包含其他包。

我个人的补充:您还可以将包裹移动到启动器所在的包裹中,或者将启动器向上移动一个包裹(这就是我所做的)。

I'll give you a solution that should work for the same error with 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.

I suffered the same problem and found the solution on this website: 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. This way you can understand why User is not found: because User is an entity class in another module. Spring Boot does not scan other packages at all;
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.

So you basically reconfigure SpringBoot to scan more packages to include the other ones.

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文