gpt4 book ai didi

Hibernate: AnnotationException: Unknown MappedBy in ... 反向引用的属性未知 (mappedBy),双向@OneToOne 关联

转载 作者:行者123 更新时间:2023-12-02 06:08:48 27 4
gpt4 key购买 nike

我有以下简单的设计:
Valid XHTML
(来源:kawoolutions.com)
这基本上是 JPA 不支持的非不相交继承的一种解决方法。人员可以单独存在,但可以选择有单个玩家实体、单个教练实体或两个实体来完成逻辑。
这是相当容易理解的 JPA 2.0 映射(注意@Id 前向关联):

@Entity
@Table(name = "Persons")
public class Person implements Serializable
{
@Id
@Column(name = "id")
private Integer id;

@Column(name = "first_name")
private String firstName;

@Column(name = "last_name")
private String lastName;

@OneToOne(mappedBy = "person")
private Coach coach = null;

@OneToOne(mappedBy = "person")
private Player player = null;

...
}

@Entity
@Table(name = "Players")
public class Player implements Serializable
{
@Column(name = "registration_nbr")
private String registrationNbr = null;

@Id
@OneToOne
@JoinColumn(name = "id")
private Person person = null;

...
}

@Entity
@Table(name = "Coaches")
public class Coach implements Serializable
{
@Column(name = "license_nbr")
private String licenseNbr = null;

@Id
@OneToOne
@JoinColumn(name = "id")
private Person person = null;

...
}
Player 和 Coach 实体类几乎相同。
这里的问题是两个双向关联(mappedBy)。这些映射与 EclipseLink 完美配合,但 Hibernate 不知何故因 AnnotationException 异常:
Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: persons] Unable to configure EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:374)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
at tld.persons.Main.main(Main.java:32)
Caused by: org.hibernate.AnnotationException: Unknown mappedBy in: tld.persons.model.Person.coach, referenced property unknown: tld.persons.model.Coach.person
at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:159)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1686)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1393)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1345)
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1477)
at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:193)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1096)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:278)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:362)
... 4 more
当切换到老式 JPA 1.0 映射时,一切都再次正常运行。 Hibernate 以某种方式似乎对上述映射有问题。
我看过很多其他的问题报告,实际上 hibernate.org 的论坛充满了类似的 Unresolved 问题,但我找不到任何关于为什么会抛出异常的线索。
对我来说,这似乎是一个 Hibernate 错误。有人可以证实这一点吗? (注意我使用的是 Hibernate 3.6)

最佳答案

如果我们忘记在persistence.xml 中声明实体类,就会遇到这个问题。

关于Hibernate: AnnotationException: Unknown MappedBy in ... 反向引用的属性未知 (mappedBy),双向@OneToOne 关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5837891/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com