gavin wrote:
You must be using identity pk generation, in which case inserts happen immediately.
I changed all of entities to use HiLo generation instead of native, but I'm still getting the same exception:
org.hibernate.PropertyValueException: not-null property references a null or transient value: com.prosc.test.Classroom.campus
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:236)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:160)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:108)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:481)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:476)
at com.prosc.test.SaveOrderTest.testSavingOrder(SaveOrderTest.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at com.intellij.rt.execution.junit2.JUnitStarter.main(JUnitStarter.java:31)
Here is the code which fails:
Code:
Session session = TestUtils.sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Classroom room = new Classroom();
Campus campus = new Campus();
room.setCampus(campus);
session.save(room); //This fails, since the campus has not been saved yet
session.save(campus);
tx.commit();
session.close();
Here are the relevant mapping files:
Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.prosc.test" default-cascade="lock" default-access="field">
<class lazy="false" name="Classroom">
<id name="id" type="java.lang.Long" unsaved-value="null">
<generator class="hilo">
<param name="table">Id_Classroom</param>
</generator>
</id>
<property name="roomNumber" type="java.lang.String"/>
<property name="seatingCapacity" type="java.lang.String"/>
<many-to-one name="campus" column="campusId" not-null="true"/>
<list name="courses" lazy="true" inverse="false">
<key column="classroomId"/>
<index column="sortOrderForclassroom"/>
<one-to-many class="Course"/>
</list>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.prosc.test" default-cascade="lock" default-access="field">
<class lazy="false" name="Campus">
<id name="id" type="java.lang.Long" unsaved-value="null">
<generator class="hilo">
<param name="table">Id_Campus</param>
</generator>
</id>
<property name="city" type="java.lang.String"/>
<property name="state" type="java.lang.String"/>
</class>
</hibernate-mapping>