I am not having success with this. Looking at the unit test for the Body/Heart one-to-one relationship it doesn't appear as if this functionality is actually tested (note how the ID is manually set in Heart):
Code:
public void testUnidirectionalTrueOneToOne() throws Exception {
Body b = new Body();
Heart h = new Heart();
b.setHeart(h);
b.setId( new Integer(1) );
h.setId( b.getId() ); //same PK
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
s.persist(h);
s.persist(b);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
b = (Body) s.get( Body.class, b.getId() );
assertNotNull(b);
assertNotNull( b.getHeart() );
assertEquals( h.getId(), b.getHeart().getId() );
tx.commit();
s.close();
}
I have two classes defined in much the same way as the Body/Heart example (except I have @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)) and I get the following error:
Code:
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save()