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.  [ 5 posts ] 
Author Message
 Post subject: How does getHibernateTemplate().save(...) work?
PostPosted: Tue Aug 31, 2004 11:09 pm 
Newbie

Joined: Mon Aug 09, 2004 10:53 am
Posts: 19
Hibernate version: 2.1

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

Debug level Hibernate log excerpt:

How does the save(..) method of Spring's HibernateTemplate really work? It makes a call to Session.save. I was able to use the find method to obtain a Product object with a primary key of productId, modify it and call save(product) on it. I expected to obtain an exception about trying to save an already existing product, but instead what happened was that the record in the database for that product was updated. I would have thought that I needed to use the update(...) method instead. This behavior seems to be that of saveOrUpdate, rather than save.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 31, 2004 11:11 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
This might be a really good question for the Spring forum/mailing-list.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 31, 2004 11:25 pm 
Newbie

Joined: Mon Aug 09, 2004 10:53 am
Posts: 19
I was actually directed here since it calls Session.save. I think what I'm observing may have to do with the flush mode of HibernateTemplate. Any pointers on this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 01, 2004 11:12 am 
Newbie

Joined: Mon Aug 09, 2004 10:53 am
Posts: 19
I'm running the following junit test :
Code:
public void testSaveAndUpdateProduct() throws Exception {
         

        PopulateProduct pProd= new PopulateProduct();
        List products =  pProd.getProducts(TEST_FILENAME);
         
        Product prod = (Product)products.get(0);
         
        String prodCode = prod.getProductCode();
        Integer srvId = prod.getServiceId();
         
  [b]      Integer prodId=catDao.saveProduct(prod); [/b]

        catDao.flush();
  prod = catDao.findProductByCode(prodCode,srvId);

        log4j.info("Name of product:" + prod.getProductName());

       String prodName = "Product Worth Updating - Treo ";
         
        prod.setProductName(prodName);

[b] catDao.saveProduct(prod); [/b]
           
         
         
        assertTrue(prod.getProductName().equals(prodName));
                assertEquals(prod.getProductId(),prodId);
         
        log4j.info("Id of product updated:" + prodId);

         
    }


The
Code:
saveProduct(prod)
method of my catalog Dao simply makes a call to

Code:
getHibernateTemplate().save(prod);


The 1st call to it generates an insert statement, which is what is expected.

However the 2nd call to it generates an update statement and I'm puzzled by this. I thought that if I wanted this behavior I'd have to use saveOrUpdate or replace the call to
Code:
getHibernateTemplate().save(prod);


by a call to

Code:
getHibernateTemplate().update(prod);


Can anyone explain this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 01, 2004 12:45 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Within the same session, a second call to save() simply no-ops.

The update is executed because you later change the productName attribute.


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