-->
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: Hibernate+ Spring Composite keys Update failure
PostPosted: Wed Aug 10, 2005 11:43 am 
Beginner
Beginner

Joined: Thu Apr 14, 2005 11:39 am
Posts: 31
Hi,
I am running a test web application using a Spring,Hibernate,Struts. I have a table without any PrimaryKeys. But the The whole record is Unique.
Table has 3 records like this.

4545 9595 1234 James Yes
4545 9595 1234 John NoValue2
4545 9595 1234 Bobby NoValue2

I have fetched the record and displayed in JSP and selected middle record for edit. Then I have updated all fileds except first one(4545)

Ex: 4545 9595 1234 John NoValue2
Now it is : 4545 8888 7777 Mitch Developer

I am setting the values to a "Data" object and passing it method in "HibernateSupport" class called "getHibernateTemplate().update(data)getHibernateTemplate().update(data);;"

It does not throw exception and it did not updated the record.

I tried with getHibernateTemplate().saveOrUpdate(data) also. But no luck.

Here is my Mapping file

<hibernate-mapping package="com.test.model">
<class name="Data" table="apenodeproperties">
<composite-id >
<key-property name="sceneid" column="SCENEID" type="long"></key-property>
<key-property name="personid" column="PERSONID" type="long"></key-property>
<key-property name="nodeid" column="NODEID" type="long"></key-property>
<key-property name="name" column="NAME" type="java.lang.String"></key-property>
<key-property name="value" column="VALUE" type="java.lang.String"></key-property>
</composite-id>
</class>
</hibernate-mapping>

The question is : How this support class know which record I updated ?.
How to resolve this issue?.
I thout to pass actual values to the DAOHelper and search the record based on old values then update that object with new values but...here I am making two calls to DB.

Ant thouts will be appreciated.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 10, 2005 12:49 pm 
Regular
Regular

Joined: Tue Mar 01, 2005 2:35 pm
Posts: 60
Try adding this line in the initDao() method of you HibernateDaoSupport subclass:

getHibernateTemplate().setFlushMode(HibernateTemplate.FLUSH_EAGER);


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 10, 2005 1:03 pm 
Regular
Regular

Joined: Thu Dec 02, 2004 7:11 am
Posts: 85
If you don't have primary key and change value of composite key, how can Hibernate deduce which one of the objects you want to update? Original session was closed, and new one don't have reference to your object.

You can use the following sequence while processing update:

1) Open transaction
2) Retrieve old object using old values
3) Update old object with new values
4) Commit transaction - changes should be saved now, because at this time session have reference to changed object.

But better design would be using surrogate primary key.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 10, 2005 2:28 pm 
Beginner
Beginner

Joined: Thu Apr 14, 2005 11:39 am
Posts: 31
pkulka,

Here is my DAO support class. But I don't have init() method.

public class DataDAOHibernate extends HibernateDaoSupport implements DataDAO {

/* (non-Javadoc)
* @see com.amfam.lifewkfl.dao.DataDAO#saveData(com.amfam.lifewkfl.model.Data)
*/
public void saveData(Data data) {
try{
getHibernateTemplate().update(data);
System.out.println("I am here ...");
}catch (DataAccessException e) {
e.printStackTrace();
}

}

/* (non-Javadoc)
* @see com.amfam.lifewkfl.dao.DataDAO#searchData()
*/
public List searchData() {
List list = new ArrayList();
try {
Query q =
getSession().createQuery("from Data d where d.sceneid=4545");
list = q.list();


} catch (HibernateException e) {
e.printStackTrace();
}
return list;

}

}
Where should I put this line ?
getHibernateTemplate().setFlushMode(HibernateTemplate.FLUSH_EAGER);

sergeya,

What you have explained is my least opetion, Cause I have to send old values from Struts Action to all the way back to DAOSupport then fetch the data with old values and update which I already mentioned in my thread that it will be two call to DB and trying to find other ways :). But this is a general scenarion, every one does like this ? I mean making calles two times for edit ?

Thanks
Sudhakar


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 11, 2005 6:13 am 
Regular
Regular

Joined: Thu Dec 02, 2004 7:11 am
Posts: 85
sudhakartv wrote:
sergeya,

What you have explained is my least opetion, Cause I have to send old values from Struts Action to all the way back to DAOSupport then fetch the data with old values and update which I already mentioned in my thread that it will be two call to DB and trying to find other ways :). But this is a general scenarion, every one does like this ? I mean making calles two times for edit ?

Thanks
Sudhakar


If you have secondary cache, then getting object by key not nessary resulted to query, but, if you didn't want select in any case, you may try following scenario:

1) Pass original unmodified object to lock method - this will associate object with session without query.
2) Modify object with new values.
3) Commit transaction

See http://www.hibernate.org/hib_docs/v3/reference/en/html/objectstate.html#objectstate-detached


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.