-->
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.  [ 2 posts ] 
Author Message
 Post subject: saving collections along with the owning entity
PostPosted: Mon Dec 06, 2004 7:31 am 
Beginner
Beginner

Joined: Thu Dec 02, 2004 4:49 am
Posts: 32
Hibernate version: 2.0

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping package="com.sat">
<class name="User" proxy="User" table="TEMP_USER">
<id name="id" type="int" column="USER_ID">
<generator class="increment"/>
</id>
<property name="adminRole" type="true_false" column="ADMIN_ROLE"/>
<property name="userName" insert="true" column="USER_NAME"/>
<set name="addresses" table="TEMP_ADDRESS" inverse="true" cascade="save-update" >
<key column="USER_ID"/>
<one-to-many class="Address"/>
</set>
</class>
<class name="Address" table="TEMP_ADDRESS">
<id name="id" type="int" unsaved-value="null" column="ADDRESS_ID">
<generator class="increment"/>
</id>
<many-to-one cascade="save-update" name="user" foreign-key="USER_ID" class="User" column="USER_ID" />
<property name="place" column="PLACE"/>
</class>
</hibernate-mapping>



Following is from my main method
User user = new User();
Address addr = new Address();
Set adds = new HashSet();
addr.setPlace("XXXX");
adds.add(addr);
user.setUserName("YYYYY");
user.setAdminRole(false);
user.setAddresses(adds);
User usr = dao.save(user);

In DAO which extends HibernateDaoSupport I have

Session sess = getSessionFactory().openSession();
Transaction txn = sess.beginTransaction();
getHibernateTemplate().save(user);
txn.commit();
sess.flush();
sess.close();
return user;





Dec 6, 2004 4:52:31 PM net.sf.hibernate.impl.SessionImpl execute
SEVERE: 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:689)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:642)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2414)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2368)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2236)
at org.springframework.orm.hibernate.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:202)
at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:151)
at org.springframework.orm.hibernate.HibernateTemplate.save(HibernateTemplate.java:240)
at com.sat.DAO.save(DAO.java:36)
at com.sat.FacadeImpl.main(FacadeImpl.java:43)
org.springframework.orm.hibernate.HibernateSystemException: SQL insert, update or delete failed (row not found); nested exception is net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
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:689)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:642)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2414)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2368)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2236)
at org.springframework.orm.hibernate.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:202)
at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:151)
at org.springframework.orm.hibernate.HibernateTemplate.save(HibernateTemplate.java:240)
at com.sat.dao.DAO.save(DAO.java:36)
at com.sat.FacadeImpl.main(FacadeImpl.java:43)


Name and version of the database you are using: DB2 7.1

The generated SQL (show_sql=true):
Hibernate: insert into TEMP_USER (ADMIN_ROLE, USER_NAME, USER_ID) values (?, ?, ?)
Hibernate: update TEMP_ADDRESS set USER_ID=?, PLACE=? where ADDRESS_ID=?


Debug level Hibernate log excerpt:


What I was trying to do is, I want to insert an User along with his address.
but here Hibernate is trying to update the Address table instead of inserting a new row...what is happening here? this is a very basic thing. plz help me find what i am missing here...

Thanks,
SSSS.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 06, 2004 11:03 am 
Beginner
Beginner

Joined: Thu Dec 02, 2004 4:49 am
Posts: 32
i got it... i should specify

unsaved-value="0"

in id attribute of Address class

Thanks,
SSSS


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