Here are two class snippets and the associated HBM files.
Code:
public class Address implements Serializable
{
protected Long addressID;
protected String street;
protected String city;
protected String state;
protected String zip;
protected Set<Person> people;
}
public class Person implements Serializable
{
protected Long personID;
protected String name;
protected Address address;
}
<class name="tests.Address" table="addresses" lazy="true">
<id name="addressID" type="java.lang.Long" column="AddressID">
<generator class="native"/>
</id>
<property name="street" type="java.lang.String" column="Street" length="128"/>
<property name="city" type="java.lang.String" column="City" length="32"/>
<property name="state" type="java.lang.String" column="State" length="2"/>
<property name="zip" type="java.lang.String" column="Zip" length="10"/>
<!-- bi-directional one-to-many association to CollectionObject -->
<set name="people" lazy="true" inverse="true" cascade="none">
<key>
<column name="AddressID"/>
</key>
<one-to-many class="tests.Person"/>
</set>
</class>
<class name="tests.Person" table="people" lazy="true">
<id name="personID" type="java.lang.Long" column="PersonID">
<generator class="native"/>
</id>
<property name="name" type="java.lang.String" column="Name" length="128"/>
<many-to-one name="address" class="tests.Address" column="AddressID" not-null="true" outer-join="true" cascade="none"/>
</class>
Assume the following code (which works without problems):
Code:
Address addr = new Address();
addr.setStreet("1345 Jayhawk Blvd");
addr.setCity("Lawrence");
addr.setState("KS");
addr.setZip("66045");
Person p = new Person();
p.setName("Bob");
p.setAddress(addr);
addr.getPeople().add(p);
When I try to persist the objects I created, Hibernate seems to be picky about the order in which objects are passed to session.saveOrUpdate(). The order shouldn't matter as long as it's all within one transaction, right?
This code throws an exception: org.hibernate.PropertyValueException: not-null property references a null or transient value: tests.Person.addressCode:
Transaction tx = s.beginTransaction();
s.saveOrUpdate(p);
s.saveOrUpdate(addr);
tx.commit();
This code works without problems.Code:
Transaction tx = s.beginTransaction();
s.saveOrUpdate(addr);
s.saveOrUpdate(p);
tx.commit();
What's going on here?