-->
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 refresh persistent object in a session ?
PostPosted: Fri May 27, 2011 7:49 am 
Newbie

Joined: Thu May 19, 2011 5:27 am
Posts: 4
Hi All

How can I refresh the persistent objects in a session, so that the object is updated with the data in the database ?

The problem is, I have 2 applications.
The 1st reads the entities with:
Code:
var Users session.Query<User>();


Then the 2nd reads the entities with the same statement and adds more datasets:
Code:
var Users2 session.Query<User>();
User user = new User("Hans", "Muster");
Users2.Add(user);
session.SaveOrUpdate(Users2);


But then, when the first application wants to add more datasets, I get the following error:
"a different object with same identifier value was already associated with the session"

I tried everything like:
Code:
session.Evict(Users)
Users = Users session.Query<User>();
Users.Add(......)
session.SaveOrUpdate(Users2);


or:
Code:
Users = Users session.Query<User>();
session.Merge(Users);
Users.Add(......)
session.SaveOrUpdate(Users2);


or:
Code:
session.Flush();
Users = Users session.Query<User>();
Users.Add(......)
session.SaveOrUpdate(Users2);


without success.

However, I could close the session and reopen it, but I don't want to do this as I want to use lazy loading which does not allow to close a session.

So please, how can a refresh or better reread data from the database into a persistent object, so that this error exception can be avoided.

Thank you for your hints
Tom


Top
 Profile  
 
 Post subject: How to refresh persistent object in a session ?
PostPosted: Wed Aug 10, 2011 5:00 am 
Newbie

Joined: Wed Aug 10, 2011 4:45 am
Posts: 3
Hi...Persistent objects exist in the database, and Hibernate manages the persistence for persistent objects.If fields or properties
change on a persistent object, Hibernate will keep the database representation up-to-date.

Hibernate provides a mechanism to refresh persistent objects from their database representation.Use one of the refresh()
methods on the Session interface to refresh an instance of a persistent object, as follows:

public void refresh(Object object) throws HibernateException
public void refresh(Object object, LockMode lockMode) throws HibernateException

_________________
Cegonsoft


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.