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)