-->
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: How to avoid an unnecessary update on just inserted object?
PostPosted: Fri Dec 17, 2004 8:38 am 
Beginner
Beginner

Joined: Mon Feb 09, 2004 6:43 am
Posts: 35
Hi,
As we have performance problems we want to avoid any unnecessary operations on database. That's why we want to avoid an update on just inserted object.

We have an object Fee that is assosciated with Contract object. One Contract can have a set of Fees.

We create a new Fee object (and assosciate it with Contract at once) .Just after that we add this Fee to a set of fees that has a Contract.
In hibernate log we can see that:
- at first a Fee is created
- later a Fee is updated (although the object is exactly the same as during insert).

Is there any method to avoid this update? I understand this update must be somehow related to setting feeSet to a Contract.

CODE:
Fee newCopy = new Fee(oldFee, newContract);
feeSet.add(newCopy);
FeeDao.getInstance().save(newCopy);
newContract.setFeeSet(feeSet);
SessionTxManager.commit();

HIBERNATE LOG:
Hibernate: insert into FEE (CONTRACT_ID, E_CONTRACT_ACCOUNT_NO, .. FEE_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: update FEE set CONTRACT_ID=? where FEE_ID=?







Hibernate version:
2.1.6

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:
Oracle 9.2.05

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 17, 2004 9:08 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Code:
Fee newCopy = new Fee(oldFee, newContract);
feeSet.add(newCopy);
FeeDao.getInstance().save(newCopy);
newContract.setFeeSet(feeSet);
SessionTxManager.commit();


newContract.addFee(...) --> to manage both association ends in one method.

if newContract is already attached to the session, do nothing (must have cascade = save-update)

if not, just call session.update(newContract).

Always think about addChild and removeChild methods when you have bidirectionnal associations.

next time please follow red card and show mapping file.

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


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.