Two tables, primary key of one is foreign key of another (Legacy DB)
I used bi-directional one to one mapping:
@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;
}
The problem is when running:
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();
}}
, it throws this exception:
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