-->
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.  [ 6 posts ] 
Author Message
 Post subject: Updates happen outside of a transaction
PostPosted: Wed Aug 17, 2005 4:57 pm 
Newbie

Joined: Fri Jun 10, 2005 2:22 pm
Posts: 5
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.5



Should the following code update the Category object or is this a bug? It seems like I’m loading the object outside of a transaction, yet it is still saved without an update call.

Code:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
      
Category category = (Category) session.get(Category.class,92);      
category.setName("CCC");      
      
Transaction transaction = session.beginTransaction();
transaction.commit();
      
session.close();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 5:34 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
the object is associated with the session and dirty - thus it will be flushed when you commit the transaction.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 5:37 pm 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
Some things about your code...

Code:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();

// 1.) This does not work. You'll have to use new Long(92), Long.valueOf(92) [Java 1.5] or whatever. Anyway you need a Serializable object
// 2.) Better: Use session.load(...), cause you assume that the object exists and non-existence would be an actual error (see JavaDoc e.g.). session.get(..) might return NULL which would lead to a NullPointerException.
Category category = (Category) session.get(Category.class, 92);
category.setName("CCC");     

Transaction transaction = session.beginTransaction();
transaction.commit();
     
session.close();


Use:
Code:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();

Category category = (Category) session.load(Category.class, new Long(92));
category.setName("CCC");     

transaction.commit();
session.close();


Best regards
Sven

_________________
Please don't forget to give credit, if this posting helped to solve your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 5:49 pm 
Newbie

Joined: Fri Jun 10, 2005 2:22 pm
Posts: 5
Sven,

Thanks for your tips. I understand that it's cleaner to load the object inside a transaction, I'm just very surprised that the update occurs when I load the object OUTSIDE the transaction.

Caster


sven wrote:
Some things about your code...

Code:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();

// 1.) This does not work. You'll have to use new Long(92), Long.valueOf(92) [Java 1.5] or whatever. Anyway you need a Serializable object
// 2.) Better: Use session.load(...), cause you assume that the object exists and non-existence would be an actual error (see JavaDoc e.g.). session.get(..) might return NULL which would lead to a NullPointerException.
Category category = (Category) session.get(Category.class, 92);
category.setName("CCC");     

Transaction transaction = session.beginTransaction();
transaction.commit();
     
session.close();


Use:
Code:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();

Category category = (Category) session.load(Category.class, new Long(92));
category.setName("CCC");     

transaction.commit();
session.close();


Best regards
Sven


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 5:51 pm 
Newbie

Joined: Fri Jun 10, 2005 2:22 pm
Posts: 5
Max,

Thanks for your help. I just want to make sure that this is normal behavior. It seems odd that an object is an updated when it's not associated with a transaction. I never realized that just associating it with a session and then creating a transaction at a later stage will have such effect.


max wrote:
the object is associated with the session and dirty - thus it
will be flushed when you commit the transaction.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 18, 2005 2:34 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
That is how automatic dirty checking works - if you don't want the transaction to include the current dirty state of the session then call session.clear() - but the best is to use transactions the way they are described in the docs (starting a transaction right after you have opened the session).

_________________
Max
Don't forget to rate


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