-->
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.  [ 4 posts ] 
Author Message
 Post subject: Persist and evict object from session
PostPosted: Fri Feb 11, 2011 4:38 pm 
Newbie

Joined: Sat Dec 11, 2010 8:49 am
Posts: 19
Hello,

How is it possible to first persist and after that evict object from the same session in Hibernate?
I cannot afford to use flush on session before evict!

In my application user can open new panel A for inserting A in database.
Then in panel A user can click to open new panel for adding bean B in database and also to
open new panel C for adding bean C from panel B.
What i need is to persist A and B when user cliks to open panel C because bean C
has foreign key to bean B and B has foreign key to A.
I need to flush session only when user clicks OK on panel A.
However if user clicks Cancel on panel B i need to evict bean B from session because i don't
want to persist bean B. However Hibernate throws exception
possible non-threadsafe access to the session when i try to evict
already persisted object. I cannot use flush beacuse it will insert B into database.

Can someone help me about this.


Top
 Profile  
 
 Post subject: Re: Persist and evict object from session
PostPosted: Fri Feb 11, 2011 6:49 pm 
Newbie

Joined: Sat Dec 11, 2010 8:49 am
Posts: 19
After some tests i find out that evict does not do what i want.
E.g.
session.setFlushMode(FlushMode.MANUAL);
Stock stock = new Stock();
stock.setStockCode("4715");
session.save(stock);

session.evict(stock);
session.flush();

This will insert stock object in database even evict is called after save.
Is there any way to remove already persisted object to session to not be
saved when session is flushed?
Let say i needed to create and persist bean in session and afterwards i decided not to
save that bean in database on session flush. Is it possible?

Any help?


Top
 Profile  
 
 Post subject: Re: Persist and evict object from session
PostPosted: Fri Feb 11, 2011 7:29 pm 
Newbie

Joined: Wed Jan 26, 2011 12:32 pm
Posts: 12
No way.
All changes till save method will execute , but the changes after SAVE won't goes to database


Top
 Profile  
 
 Post subject: Re: Persist and evict object from session
PostPosted: Sat Feb 12, 2011 3:30 am 
Newbie

Joined: Sat Dec 11, 2010 8:49 am
Posts: 19
I can see that, but what is then proper way to handle this?

I need to persist object A in one panel to open new panel from first panel (beacuse bean in new panel
have foregin key to bean in first panel) and so on.
Now if user decide to insert beans in sub-panels and to not insert bean in first panel all
persisting should be discarded without any insert in database and that is ok beacuse
i am calling flush only on first panel. But problem is when user decite to not insert
beans in sub-panels and to insert bean in first panel. As i flush session here, all
previously persisted objects will be in database and i want to avoid this.

Any ideas?


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