-->
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.  [ 3 posts ] 
Author Message
 Post subject: auto-save on query-execution
PostPosted: Wed Apr 26, 2006 4:27 am 
Newbie

Joined: Tue Apr 25, 2006 4:18 pm
Posts: 2
Hey,

im quite new to hibernate and got a problem. In example i have a table with some rows that are stored persistent by hibernate. I get the objects by a "public List getAll()"-method. This one is execution something like this:

Code:
Session s = SessionFactory.openSession();
Transaction tx = s.beginTransaction();

Query query = getSession().createQuery("SELECT o FROM <class> AS o");
List result = new ArrayList(query.list());

tx.commit();



I have a 3-tier-architecture and wants to be able to save and refresh the data. The problem is now, that when i edit an item within the List of getAll() and do a refresh by another getAll()-call (i want to loose the changes and reload the entries from the DB) it does save the changes i made automatically and thus a new getAll() gets the the changed items again.

Can someboy can tell me how i can disable this auto-save ? I want to implicitly save the list by a save/update-call.

The session use is created by a singleton-pattern and i use the same session within each getAll()-call. Should i close it right after one query ?


Hibernate version: 2.1

Name and version of the database you are using: PostgreSQL 8.0



Thanks for the help,
oliver


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 26, 2006 5:02 am 
Newbie

Joined: Tue Oct 25, 2005 6:04 am
Posts: 10
Your changes are not posted to the database (flushed) until you commit your transaction or do a session.flush(). If you want to refresh an object (re-read into it the data from the database), you need to call session.refresh(object).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 26, 2006 5:52 am 
Newbie

Joined: Tue Apr 25, 2006 4:18 pm
Posts: 2
Hello kresho,

i do a tx.commit() but just within this:


Code:
Session s = SessionFactory.openSession();
Transaction tx = s.beginTransaction();

Query query = getSession().createQuery("SELECT o FROM <class> AS o");
List result = new ArrayList(query.list());

tx.commit();



As you see there im only do a query that do a SELECT, so i want to get all objects from table <class>. This code is from a method like "List getAll();"

I do no other call but this getAll() and change one object within the list in between, like here:

List list = getAll();
list.get(0).setSomething(0815);
list = getAll();


After this call he saved the value 0815 within the database automatically, without any save-call from my side. But i need to have it unchanged.

That refresh-method dont look too comfortable for my needs at this point as i have my list and would have to refresh every item in it and delete any new one.

oliver


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