-->
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.  [ 8 posts ] 
Author Message
 Post subject: How can I just flush one entity, and not others
PostPosted: Thu Jan 08, 2009 11:31 pm 
Newbie

Joined: Thu Jan 08, 2009 11:23 pm
Posts: 2
Location: China
How can I just flush one entity, so it's can be queried before commit, which will flush everything.

I konw createSQLQuery can do this, but is there any more convenience methods available?

example:(What I want is 'tela' can be query in same Transaction it's saved. Can't flush everything, the Transaction have other things to do.)
session.beginTransaction();
TTelephone tela = new TTelephone();
tela.setCitycode("jx");
tela.setAreaCode("0573");
tela.setTelephone("0000");
tela.setAddress("tela");
session.save(tela);

TTelephone telb = telephoneDao.findByTel("0000").get(0);
System.out.println(tela);
System.out.println(telb);
System.out.println(tela == telb);
session.getTransaction().rollback();


Last edited by neoscript on Fri Jan 09, 2009 2:06 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2009 12:05 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
use session.evict() method for flushing only one entity or entity collection.

for er.,

session.evict(Users.class);

u can also use sessionFactory for this...

sessionFactory.evict(Users.class);

_________________
If u feel it will help you, don't forget to rate me....


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2009 12:14 am 
Newbie

Joined: Thu Jan 08, 2009 11:23 pm
Posts: 2
Location: China
Changes to the instance will not be synchronized with the database

/**
* Remove this instance from the session cache. Changes to the instance will
* not be synchronized with the database. This operation cascades to associated
* instances if the association is mapped with <tt>cascade="evict"</tt>.
*
* @param object a persistent instance
* @throws HibernateException
*/
public void evict(Object object) throws HibernateException;


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2009 12:53 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
ur requirement is to get latest data from DB. is it?

if ur answer is yes then, use evict() method.


*) i can't understand ur quest.

_________________
If u feel it will help you, don't forget to rate me....


Top
 Profile  
 
 Post subject: Re: How can I just flush one entity, and not others
PostPosted: Fri Jan 09, 2009 12:58 am 
Regular
Regular

Joined: Wed Apr 25, 2007 11:44 pm
Posts: 59
neoscript wrote:
How can I just flush one entity, so it's can be queried before commit, which will flush everything.

I konw createSQLQuery can do this, but is there any more convenience methods available?


i dont think so its a good idea


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2009 1:02 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
there is no methods to flush() single entity. so use evict() to remove instance from cache and then when will u query it returns new data (i.e., hit from DB not from cache).

i m used this technique in my project, i works fine.


u may also use refresh() ..

void refresh(Object object)
--- Re-read the state of the given instance from the underlying database.

where object in this method, use ur entity.

_________________
If u feel it will help you, don't forget to rate me....


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2009 6:23 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
there is no methods to flush() single entity. so use evict()....

i m used this technique in my project, i works fine.


But it will not work for neoscript since it is a new entity that is scheduled for insertion with session.save(). There is no information in the database about this object.

Quote:
there is no methods to flush() single entity


As far as I know this is correct. A flush() always flushes everything.

However I know that Session.save() does an immediate insert with some identifier generators (eg. those that depend on the database for generating the ID). But it also seems like there is going to be some changes with respect to this in Hibernate 3.4 (http://opensource.atlassian.com/project ... e/HHH-2439)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2009 6:35 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Why is it not possible to insert the newly created entity in another transaction? This would be the normal solution. What if your "big" transaction is rolled back? Should the newly created instances, which you want to be saved immediately, be deleted?


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