两个表,一个主键是另一个(Legacy DB)的外键 我使用了双向一对一映射:
@Entity
public class First {
@Id protected int a;
@OneToOne(mappedBy ="first", cascade = CascadeType.PERSIST)
@JoinColumn(name = "a")
protected Second second;
}
@Entity
public class Second {
@Id protected int a;
@OneToOne
@JoinColumn(name = "a", insertable = false)
First first;
}
问题出在运行时:
public class Persister {
public static void main(String[] args) {
EntityManagerFactory aEntityManagerFactory;
EntityManager aEntityManager;
aEntityManagerFactory =
Persistence.createEntityManagerFactory("bibit_notification_jpa");
aEntityManager = aEntityManagerFactory.createEntityManager();
Second aSecond = new Second();
aSecond.a = 1;
First aFirst = new First();
aFirst.a = 1;
aFirst.second = aSecond;
aEntityManager.getTransaction().begin();
aEntityManager.persist(aFirst);
aEntityManager.getTransaction().commit();
}}
,它抛出了这个异常:
Exception in thread "main" javax.persistence.PersistenceException: java.lang.NullPointerException at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:252) at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:120) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33) at Persister.main(Persister.java:14) Caused by: java.lang.NullPointerException at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:135) at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1130) at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:296) at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1115) at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1233) at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:154) at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:869) at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:183) at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:240) ... 4 more
答案 0 :(得分:2)
这篇文章:http://forum.hibernate.org/viewtopic.php?t=970823)
似乎表明注释3.2.x存在某种问题。你用的是什么版本?如果是3.2.x,请尝试使用3.3或3.4。
答案 1 :(得分:1)
您可能需要查看JavaDoc中的第二个示例。如果我读得正确,代码应该是
@Entity
public class First {
@Id protected int a;
@OneToOne @PrimaryKeyJoinColumn
protected Second second;
}
@Entity
public class Second {
@Id protected int a;
@OneToOne @PrimaryKeyJoinColumn
First first;
}