-->
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: Problem when querying after create or update
PostPosted: Thu Jun 28, 2007 4:43 pm 
Newbie

Joined: Wed Jun 27, 2007 5:09 pm
Posts: 7
I have service method call createProduct():
Code:
public void createProduct()
{
     Product product1 = new Product();
     //set product1 attributes
     session.save(product1);
}


Another service method call listProducts():
Code:
public List<Product> listProducts()
{
    Criteria criteria = session.createCriteria(Product.class).
    //return all products
    List productList = criteria.list();
    return productList;
}


The above 2 service calls are made in the same transaction context. i.e. first create the product and get a listing of products.

The problem is, productList listing contains "product1" instance in memory(created from createProduct()) instead of reading corresponding row from DB.
The same happens when a product is edited and then the DB is queried. The query contains the edited object in memory.

How can this happen ? Has anyone encountered this situation. How do I solve this problem.

Note:- I'm using Spring's declarative transaction management. I'm also using OpenSessionViewFilter. Therefore FLUSH_MODE = NEVER.

Any help is appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 28, 2007 6:57 pm 
Newbie

Joined: Thu Nov 09, 2006 9:42 pm
Posts: 13
Hello,

If you want to update a product in memory with the current database state you can use EntityManager.merge or EntityManager.refresh. Be careful to manage your OneToMany and ManyToOne relationships if you do this. You may want to look into the details of Hibernate's level two cache as well.

--
Tim


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.