-->
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: Queries from HQL
PostPosted: Thu Mar 12, 2009 2:45 pm 
Newbie

Joined: Thu Mar 12, 2009 1:59 pm
Posts: 2
How is it, if I have a HQL-Query like this


Code:
SELECT pojo FROM Pojo WHERE ...


and I get the result like this

Code:
Iterator itr = sess.createQuery( hql ).iterate();
Pojo pojo= (pojo)itr.next();


Is the result object in the state "persistence"?
If I change the object how can I tell hibernate to apply these changes in the database as well?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 2:54 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
Is the result object in the state "persistence"?


Yes.

Quote:
If I change the object how can I tell hibernate to apply these changes in the database as well?


By committing the transaction. The common pattern is (copied from Javadoc of Session class):

Code:
Session sess = factory.openSession();
Transaction tx;
try {
     tx = sess.beginTransaction();
     //do some work
     ...
     tx.commit();
}
catch (Exception e) {
     if (tx!=null) tx.rollback();
     throw e;
}
finally {
     sess.close();
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 3:11 pm 
Newbie

Joined: Thu Mar 12, 2009 1:59 pm
Posts: 2
Ok, thank for the answer.

And I guess if I select only a property of my class the changes would not effect the database after a commit. Am I right?

Code:
SELECT pojo.mystring FROM Pojo WHERE ...


I just tried it and it seem only to work with Pojos which I defined in a mapping file.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 3:14 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
And I guess if I select only a property of my class the changes would not effect the database after a commit. Am I right?


Yes, that is correct.


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.