-->
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.  [ 8 posts ] 
Author Message
 Post subject: How to update only a few columns of detached entity
PostPosted: Wed Aug 24, 2005 8:57 am 
Newbie

Joined: Tue Aug 17, 2004 11:07 am
Posts: 3
I am new to Hibernate and I am getting stuck with a basic but tricky issue about Hibernate and ORM at all.

Supose a have a system with a "Product" entity with properties such as "id", "description" and "price" among others. The Product entity is mapped to a default table Product with similar columns. Now, the tricky scenario:

1- the system list products and display to the user only id, name and price fields
2- the user selects a product from a list and change the price of the product
4- the system updates the product

In step 1, the system opens a session, select the entities and close the session.
In step 4, the system opens a new session and call a update(product).
But since the entity was contructed outside a session and has given values only to the filelds "id" and "price" and other fields remain blank (were not read from database in previous load), the update will actually turn blank these other fields in the database after the update.

In a JDBC application, I would issue a <b>select id, description, price from product</b> int step 1 and later a <b>update product set price=? where id=?</b> in step 4

How can I do the same with Hibernate? What is the recommended way of playing with such a typical scenario using Hibernate? The only way I can think is loading the entire entity in step 4, copying the changing fields from user input and updating the entity in the same session. But sure this is not the most eficient way from the database point of view, is it?


Thanks in advance.
Charles


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 9:51 am 
Beginner
Beginner

Joined: Mon Apr 12, 2004 6:33 pm
Posts: 35
Why are the other fields blank? A detached object still retains its values.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 9:59 am 
Beginner
Beginner

Joined: Mon Aug 15, 2005 4:37 pm
Posts: 27
Location: Washington DC
kdekooter wrote:
Why are the other fields blank? A detached object still retains its values.


Yes, it seems like your retrieve itself is missing values.
Check your database for the data.

And if you can list your code sample, it might be easier to have see what the problem is.

For further reference, check this link and see if it helps.
http://www.hibernate.org/hib_docs/v3/re ... e-detached


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 11:14 am 
Newbie

Joined: Tue Aug 17, 2004 11:07 am
Posts: 3
Sorry, I'will try to explain better using JDBC+SQL idiom, since I'm trying to switch from it to ORM using Hibernate.

I need to display a list of products to the user for he can select one and change its price. In that list of products, I want only to show product id, description and price, although there are other fields such as manufacturer, weight, color, etc in the Product table. With pure JDBC:

1- I open a connection (or get it from pool)
2- I run a select p.id, p.description, p.price from Product p query
3- I extract data from resultset, show in a screen to the user and close the connection
4- I get input from user (id and new price)
5- I open a connection
6- I run update Product set price=? where id=?

So, with Hibernate I'm trying to do:

1- I open a session
2- I run a select p.id, p.description, p.price from Product p query by query.list()
3- I extract data from List of Object[] returned, show in a screen to the user and close the session
4- I get input from user (id and new price)
5- I create a new instance of Product and give it the data from user input

Product p = new Product();
product.id = screen.productId;
product.description = screen.productDescription;
product.price = screen.productPrice;

6- I run a session.update(p)

Here, after step 6, Product table' columns color, weight and manufacturer turn into its default blank (null or 0) values. I think that occurs because when Hibernate take that product instance to save it, it cannot know if color, weight etc fields in the instance have that null or zero values because (a) user or application changed it or (b) because it should be ignored in the update.
So, how is the correct way of doing things? Note I'm reading data in one session and updating it in another session. Note I'm not interested in loading all columns from the table to show only a few to the user.

Hope you can get it. Sorry for the long post.
Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 5:36 pm 
Beginner
Beginner

Joined: Mon Aug 15, 2005 4:37 pm
Posts: 27
Location: Washington DC
chlabreu wrote:
Sorry, I'will try to explain better using JDBC+SQL idiom, since I'm trying to switch from it to ORM using Hibernate.
......
So, with Hibernate I'm trying to do:

1- I open a session
2- I run a select p.id, p.description, p.price from Product p query by query.list()
3- I extract data from List of Object[] returned, show in a screen to the user and close the session
4- I get input from user (id and new price)
5- I create a new instance of Product and give it the data from user input

Product p = new Product();
product.id = screen.productId;
product.description = screen.productDescription;
product.price = screen.productPrice;

6- I run a session.update(p)
....


Hi,

I think you are missing the concept of using detached instances. You don't need to create a new Product instance and set the new values. Try setting the values on the Product instance you used during the first session and then perform the udpate() call on the product in a different session and transaction. That should take care of the update and it should perform the update on the existing product record.

Product p = new Product(); // Comment this out and use the Product instance from the previous session.
product.id = screen.productId;
product.description = screen.productDescription;
product.price = screen.productPrice;


Surya


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 6:09 pm 
Newbie

Joined: Tue Aug 17, 2004 11:07 am
Posts: 3
Quote:
You don't need to create a new Product instance and set the new values. Try setting the values on the Product instance you used during the first session and then perform the udpate() call on the product in a different session and transaction.


In fact I'm new to the concepts and I think I missguided, but what if I do not want, for performance considerations, load all the columns from the Product table in the first session? What if the columns I need load from the Product table vary from place to place in my application and I don't need to load all the columns in every place?

PS: I'm talking about a Product entity, but this is a generic problem I see in various entities in my application and in many others I can think. When using jdbc+sql, I can simply issue selects and updates against the exact columns I need at each place. But, I think I don't know how to acomplish this with Hibernate in a generic sense and couldn't found detailed explanations about this in docs or faqs.

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 6:15 pm 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
I think you are looking for the Update/Delete hql statements

[url]http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#batch-direct
[/url]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 26, 2005 2:16 am 
Regular
Regular

Joined: Sun May 08, 2005 2:48 am
Posts: 118
Location: United Kingdom
chlabreu wrote:
PS: I'm talking about a Product entity, but this is a generic problem I see in various entities in my application and in many others I can think. When using jdbc+sql, I can simply issue selects and updates against the exact columns I need at each place. But, I think I don't know how to acomplish this with Hibernate in a generic sense and couldn't found detailed explanations about this in docs or faqs.


Yes in JDBC you can naturally have two long-running editors of one database row but editing completely different unrelated columns and the business logic involved is not inter-dependant.


I'm running into little brick walls where a whole bunch of stuff would be really great for the programmer to use Java objects (over JDBC). I would like hibernate to re-use all its knowledge about the mapping of an object to columns to allow for a simple partial row INSERT/UPDATE by primary key.

I would have throught in the core of Hibernate somewhere this already exists, maybe it just needs exposing in a safe way.

I'd then like it to use prepared statements even for partial row operations. I'd like the application to have some control over marking each operation with a caching hint and LRU so the prepared statements cache does not fill up.

MyObject myObject = new MyObject();
myObject.setId(1);
myObject.setMyValue(2);
myObject.setAnotherValue("foobar");
myObject.setThirdValue("three");
myObject.setIgnoredValue("I did not mark this as interesting so hibernate will ignore this bogus value");

HibernateEditOneRecordControlObject eor = new HibernateEditOneRecordControlObject();
eor.addInteredtedColumn("my_value"); // by column name
eor.addInterestedProperty("anotherValue"); // by object property name
eor.addInterestedPropertyNativeSQL("thirdValue", "SERVER_FUNC(?)", "foo");
Session.saveOrUpdate(myObject, eor);

Hibernate then just invalidate any copy of the object it has in cache by that primary key id, the object passed to hibernate would never be attached to the session. Infact this is probably a new object state like "partial" since its not a proper detached object.


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