I'm having an issue I cannot seem to find a solution for. I am hoping someone here can help.
I put together a very simple application to reproduce the issue outside of my real project. .
I am using Hibernate 2.1.8.
I have a LeftHand class, and a RightHand class.
Each class has a property for the other class.
For example...
rightHand.setLeftHand(leftHand);
leftHand.setRightHand(rightHand);
Both objects use simple native id generator mappings, so nothing special there. The goal I'm trying to achieve is this.
A LeftHand table, with a column for the RightHand id.
A RightHand table with a column for the LeftHand id.
Each object should it's own id primary key (no export from lefthand).
The rightHand property on LeftHand should be nullable, but unique.
RightHand objects must have a LeftHand object to exist.
Here are my mappings...
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping default-cascade="save-update">
<class name="LeftHand">
<id name="id">
<generator class="native"/>
</id>
<many-to-one name="rightHand" class="RightHand" cascade="all" unique="true" />
</class>
</hibernate-mapping>
RightHand
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping default-cascade="save-update">
<class name="RightHand">
<id name="id">
<generator class="native"/>
</id>
<many-to-one name="leftHand" class="LeftHand" unique="true" not-null="true" cascade="none" />
</class>
</hibernate-mapping>
Now, if I run a little test...
Code:
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
conf.addClass(LeftHand.class);
conf.addClass(RightHand.class);
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession();
RightHand rh = new RightHand();
LeftHand lh = new LeftHand();
lh.setRightHand(rh);
rh.setLeftHand(lh);
session.save(lh);
}
It yacks with the following...
Code:
net.sf.hibernate.PropertyValueException: not-null property references a null or transient value: RightHand.leftHand
at net.sf.hibernate.impl.SessionImpl.checkNullability(SessionImpl.java:1287)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:939)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:868)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:786)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:749)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1398)
at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:901)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:868)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:786)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:749)
at Main.main(Main.java:32)
I don't understand why saving LeftHand isn't cascading to RightHand. I've been messing with this far too long and appreciate any help here.
Thanks
-Ray