-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Does order of calling saveOrUpdate matter?
PostPosted: Thu Nov 09, 2006 6:20 pm 
Newbie

Joined: Mon Jul 24, 2006 3:21 pm
Posts: 14
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.address
Code:
        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?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 20, 2006 6:44 pm 
Newbie

Joined: Fri Apr 07, 2006 7:23 pm
Posts: 9
My understanding of this is that when it persists the Person object it attempts to fill in the "address" property/column with the persistent ID of the associated Address object. But because you have not saved it yet, it doesn't have a valid persistent ID so hibernate doesn't know what value to save in that column. The Address object is not assigned its persistent ID until it is saved. The fact that you are in a transaction is irrelevant to the act of generating and setting ID's.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 20, 2006 10:17 pm 
Newbie

Joined: Mon Nov 20, 2006 10:12 pm
Posts: 2
and it will be good if you make cascade="save-update" rather than "none"


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 20, 2006 10:20 pm 
Newbie

Joined: Mon Nov 20, 2006 10:12 pm
Posts: 2
which means that when persisting one object with referrence to other object that has not been persisted, persisted other object, too. And maybe persist other object "first"... ^_^


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 21, 2006 5:49 am 
Newbie

Joined: Sun Apr 04, 2004 4:22 am
Posts: 7
Save or update Person first WITHOUT setting the Adress propertie.
Then set the Address propertie. The Address should then be persisted "automatically".


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.