-->
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: Bulk Update on cached Entity
PostPosted: Wed Nov 05, 2008 12:31 pm 
Newbie

Joined: Thu May 22, 2008 9:48 am
Posts: 13
Section 13.4. DML-style operations states that
Quote:
As already discussed, automatic and transparent object/relational mapping is concerned with the management of object state. This implies that the object state is available in memory, hence manipulating (using the SQL Data Manipulation Language (DML) statements: INSERT, UPDATE, DELETE) data directly in the database will not affect in-memory state. However, Hibernate provides methods for bulk SQL-style DML statement execution which are performed through the Hibernate Query Language (HQL).


This implies to me that HQL DML-style operations actually affect in-memory states. However, when I run my test code (below), I always see updates against the database, which I was hoping the HQL expression would run against cached entities. This is especially true in that this is NOT a bulk update, but rather updating using the entity's ID. The most important issue to me is that, with it running against the database, I shouldn't have to call refresh on my attached entity.

Could someone clarify this behavior for me?

This code works properly, but removing the refresh causes it to fail.
Code:
      Query query = null;
      
      //get record
      Map<String,Object> swAssessment = (Map<String,Object>) sessionFactory.getCurrentSession().
         load("SwAssessment", "5A3C84EBE05E3530E040007F01000B40");

      //Modify fields
      swAssessment.put("CGrossCliSs", new Integer(44));
      swAssessment.put("CGrossCliRr", new Integer(46));
                swAssessment.put("CGrossCliSubt", new Integer(9999));
      logger.info("CGrossCliSs:" + swAssessment.get("CGrossCliSs"));
      logger.info("CGrossCliRr:" + swAssessment.get("CGrossCliRr"));
      logger.info("CGrossCliSubt:" + swAssessment.get("CGrossCliSubt"));
      
      //update via HQL
      query = sessionFactory.getCurrentSession().
         createQuery("update SwAssessment set " +
               "CGrossCliSubt = CGrossCliSs + CGrossCliRr" +               
               " where guid = :guid").
         setString("guid", "5A3C84EBE05E3530E040007F01000B40");
      query.executeUpdate();
      
      sessionFactory.getCurrentSession().refresh(swAssessment);

      logger.info("CGrossCliSs:" + swAssessment.get("CGrossCliSs"));
      logger.info("CGrossCliRr:" + swAssessment.get("CGrossCliRr"));
      logger.info("CGrossCliSubt:" + swAssessment.get("CGrossCliSubt"));
      
      //see if record reflects update
      assertEquals(new Integer(90), swAssessment.get("CGrossCliSubt"));


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 2:39 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Hibernate DML operations affects the database only. It doesn't affect in-memory objects. That is why you need to call Session.refresh(). The text you are referring to means that although Hibernate is primarily an object/relational persistence service it does provide DML operations that works directly with the database.


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.