-->
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.  [ 3 posts ] 
Author Message
 Post subject: UPDATE instead of INSERT occurring
PostPosted: Wed Apr 21, 2004 11:46 am 
Newbie

Joined: Mon Mar 29, 2004 7:10 pm
Posts: 16
Location: Minneapolis, MN/Topeka, KS
Environment:

Hibernate version = 2.1.2
Database = DB2-UDB v8.1
Java Env = Eclipse 3.0 (M8) jdk 1.4.2

Problem: If I attempt to create a person, and add NEW addresses to a NEW Person and then save the person with the associated collection (using
Code:
session.save(p)
) it fails because an UPDATE is being generated by Hibernate instead of an INSERT.

However -- if I save the components separately,
Code:
session.save(addr1);
session.save(addr2);
session.save(p);

then everything works ok, but on performing session.load(Person.class, pid) -- I dont get any addresses back.

Obviously, I am doing something wrong -- but ???
thanks for any and all help.
Michael

Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<!-- $Id: Address.hbm.xml,v 1.3 2004/04/20 16:06:07 emm Exp $ -->
<hibernate-mapping>
    <class
       name="org.srs.domain.Address"
       table="ECP.ADDRESS"
       dynamic-update="true" >
      <id name="addressID" type="long" unsaved-value="null" column="ADDR_ID"  >
         <generator class="assigned" />
      </id>
      <many-to-one
         name="party" 
         column="FK_PTY_ID"
         not-null="true"
         cascade="save-update"
         />
       <property name="addrType"     column="ADDR_TYPE"     not-null="true"    />
       <property name="line1"       column="STREET1"       not-null="true"      />
       <property name="line2"       column="STREET2"       not-null="false"   />
       <property name="city"       column="CITY"           not-null="true"       />
       <property name="state"     column="STATE"        not-null="true"      />
       <property name="strNum"     column="STR_NUM"       not-null="true"     />
       <property name="zip1"       column="POSTAL_CODE1"    not-null="true"     />
       <property name="zip2"       column="POSTAL_CODE2"    not-null="false"    /> 
         
    </class>
</hibernate-mapping>


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<!-- $Id: Party.hbm.xml,v 1.6 2004/04/20 16:02:54 emm Exp $ -->
<hibernate-mapping package="org.srs.domain" >
    <class name="org.srs.domain.SRSParty" table="ECP.PARTY" >
      <id name="partyID"
         type="long"
           column="PARTY_ID"
         unsaved-value="any" >
         <generator class="assigned"/>
      </id>
        <property
           name="partyType"
           type="integer"
           column="PARTY_TYPE"/>
    <joined-subclass name="org.srs.domain.Person"
       extends="org.srs.domain.Party"
       table="ECP.PERSON" 
    >
        <key column="ID" />
        <set
          name="addresses"
          inverse="true"
          lazy="false"
          cascade="all"
        >
            <key column="ADDR_ID" />
            <one-to-many class="org.srs.domain.Address" />   
      </set>
      <property name="dob"      type="date"    not-null="true" column="DOB"/>
      <property name="name"     type="string"  not-null="true" length="30"   column="NAME" />
      <property name="LName"    type="string"  not-null="true" length="30"   column="LNAME" />
      <property name="sex"      type="string"  not-null="true" length="1"    column="SEX" />
      <property name="SSN"      type="string"  not-null="true" length="10"   column="SSN" />
      </joined-subclass>
   </class>
   
</hibernate-mapping>


----------------Code snippet--------------

Code:
p.addAddress(addr1);
        p.addAddress(addr2);
        // now save everything.
        //
        session = (Session) aFactory.openSession();
        tx = session.beginTransaction();
//        session.save(addr1);
//        session.save(addr2);
        session.saveOrUpdate(p);
        tx.commit();
        session.close();



------------StackTrace With Console log --------------------

Code:
Hibernate: select keyvalue0_.ID as ID0_, keyvalue0_.KEYVALUE as KEYVALUE0_ from ECP.KEYGEN keyvalue0_ where keyvalue0_.ID=?
Hibernate: update ECP.KEYGEN set KEYVALUE=? where ID=?
Person Key is: 17
Hibernate: select keyvalue0_.ID as ID0_, keyvalue0_.KEYVALUE as KEYVALUE0_ from ECP.KEYGEN keyvalue0_ where keyvalue0_.ID=?
Hibernate: update ECP.KEYGEN set KEYVALUE=? where ID=?
Hibernate: select keyvalue0_.ID as ID0_, keyvalue0_.KEYVALUE as KEYVALUE0_ from ECP.KEYGEN keyvalue0_ where keyvalue0_.ID=?
Hibernate: update ECP.KEYGEN set KEYVALUE=? where ID=?
Hibernate: insert into ECP.PARTY (PARTY_TYPE, PARTY_ID) values (?, ?)
Hibernate: insert into ECP.PERSON (DOB, NAME, LNAME, SEX, SSN, ID) values (?, ?, ?, ?, ?, ?)
Hibernate: update ECP.ADDRESS set FK_PTY_ID=?, ADDR_TYPE=?, STREET1=?, STREET2=?, CITY=?, STATE=?, STR_NUM=?, POSTAL_CODE1=?, POSTAL_CODE2=? where ADDR_ID=?
10:32:54,827 ERROR SessionImpl:2343 - Could not synchronize database state with session
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
   at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
   at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:688)
   at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:641)
   at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
   at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2382)
   at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2336)
   at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2204)
   at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
   at org.srs.test.TestPersonWithAssignedIndices.main(TestPersonWithAssignedIndices.java:126)
3065 [main] ERROR net.sf.hibernate.impl.SessionImpl  - Could not synchronize database state with session
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
   at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
   at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:688)
   at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:641)
   at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
   at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2382)
   at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2336)
   at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2204)
   at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
   at org.srs.test.TestPersonWithAssignedIndices.main(TestPersonWithAssignedIndices.java:126)
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
   at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
   at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:688)
   at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:641)
   at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
   at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2382)
   at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2336)
   at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2204)
   at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
   at org.srs.test.TestPersonWithAssignedIndices.main(TestPersonWithAssignedIndices.java:126)


_________________
Michael McConnell


Top
 Profile  
 
 Post subject: INSERT now occurring
PostPosted: Wed Apr 21, 2004 1:20 pm 
Newbie

Joined: Mon Mar 29, 2004 7:10 pm
Posts: 16
Location: Minneapolis, MN/Topeka, KS
I had inadvertantly set unsaved-value="null" instead of "any".

It is now performing the insert correctly without having to separately save each object, which is really nice --

However, when I now do:
Code:
session1 = (Session) aFactory.openSession();
        tx = session1.beginTransaction();
        addr1 = null;
        addr2 = null;
        p = null;
        p = (Person) session1.load(Person.class, personKey);
        Set addresses = p.getAddresses();
        Iterator i = addresses.iterator();
        while (i.hasNext()) {
          System.out.println("Address id: "
              + ((Address) i.next()).getAddressID().longValue());
        }
        System.out.println(((Person)p).printOn(1, new String()));

It only returns the Person object without any Addresses in the collection.

Modified mapping docs follow:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<!-- $Id: Address.hbm.xml,v 1.3 2004/04/20 16:06:07 emm Exp $ -->
<hibernate-mapping>
    <class
       name="org.srs.domain.Address"
       table="ECP.ADDRESS"
       >
      <id
         name="addressID"
         type="long"
         unsaved-value="any"
         column="ADDR_ID">
         <generator class="assigned" />
      </id>
      <many-to-one
         name="party" 
         column="FK_PTY_ID"
         not-null="true"
         cascade="save-update"
         />
       <property name="addrType"     column="ADDR_TYPE"     not-null="true"    />
       <property name="line1"       column="STREET1"       not-null="true"      />
       <property name="line2"       column="STREET2"       not-null="false"   />
       <property name="city"       column="CITY"           not-null="true"       />
       <property name="state"     column="STATE"        not-null="true"      />
       <property name="strNum"     column="STR_NUM"       not-null="true"     />
       <property name="zip1"       column="POSTAL_CODE1"    not-null="true"     />
       <property name="zip2"       column="POSTAL_CODE2"    not-null="false"    /> 
    </class>

</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<!-- $Id: Party.hbm.xml,v 1.6 2004/04/20 16:02:54 emm Exp $ -->
<hibernate-mapping package="org.srs.domain" >
    <class name="org.srs.domain.Party" table="ECP.PARTY" >
      <id name="partyID"
         type="long"
           column="PARTY_ID"
         unsaved-value="any" >
         <generator class="assigned"/>
      </id>
        <property name="partyType" type="integer" column="PARTY_TYPE"/>
       <set
          name="addresses"
          inverse="true"
          lazy="false"
          cascade="all"
        >
            <key column="ADDR_ID" />
            <one-to-many class="org.srs.domain.Address" />   
      </set>
   
    <joined-subclass name="org.srs.domain.Person"
       extends="org.srs.domain.Party"
       table="ECP.PERSON" 
    >
        <key column="ID" />
       
      <property name="dob"      type="date"    not-null="true" column="DOB"/>
      <property name="name"     type="string"  not-null="true" length="30"   column="NAME" />
      <property name="LName"    type="string"  not-null="true" length="30"   column="LNAME" />
      <property name="sex"      type="string"  not-null="true" length="1"    column="SEX" />
      <property name="SSN"      type="string"  not-null="true" length="10"   column="SSN" />
      </joined-subclass>
   
      
   </class>
   
</hibernate-mapping>

_________________
Michael McConnell


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 22, 2004 3:52 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Please read
http://www.hibernate.org/155.html

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.