我有以下简单的设计:
Valid XHTML http://www.kawoolutions.com/media/persons-roles-reduced.png
这基本上是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上的论坛充满了类似的未解决的问题,但是我找不到任何关于为什么会抛出异常的线索。
对我而言,它似乎是一个Hibernate错误。任何人都可以证实吗? (注意我正在使用Hibernate 3.6)
答案 0 :(得分:11)
如果我们忘记在 persistence.xml 中声明实体类,则会遇到此问题。
答案 1 :(得分:0)
可怕的错误。正如我所说EclipseLink没有任何问题。在这里查看错误报告(并投票):
http://opensource.atlassian.com/projects/hibernate/browse/HHH-5695
答案 2 :(得分:0)
确保注记类中的引用属性名称和setter getter方法名称应该相同 例如: 我有两个classess Cat和Dog有一对一的关系
在Cat课程中
class Cat {
private Dog doggy;
@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
public Dog getdoggy() {
return this.doggy;
}
public void setdoggy(Dog doggy) {
this.doggy= doggy;
}
}
in Dog class
class Dog {
private Cat cat;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "doggy")
public Cat getCat() {
return this.cat;
}
public void setCat(Cat cat) {
this.cat= cat;
}
}