File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate - update specific field with unique id Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate - update specific field with unique id" Watch "Hibernate - update specific field with unique id" New topic
Author

Hibernate - update specific field with unique id

Bhavin Sanghani
Ranch Hand

Joined: Dec 17, 2003
Posts: 67
Hi,

I want to fire query using Hibernate to update specific field. Here, I dont have object in the memory so, other fields will have null value.

If I try SaveOrUpdate() call then it will overwrite null values to other fields.

I can retrieve the object, populate it and then fire saveOrUpdate() but this looks me inefficient.

I can use SQL query but I am wondering if any better approach is available.

Please guide.

Thanks,
Bhavin
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


I can use SQL query but I am wondering if any better approach is available.

Yes, you could use an HQL query, then your code is cross platform.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Bhavin Sanghani
Ranch Hand

Joined: Dec 17, 2003
Posts: 67
Hi,

Other than cross platform thing, should we consider cost of loading object before updating it?

Thanks
Bhavin
[ December 09, 2008: Message edited by: Bhavin Sanghani ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Probably not. The best approach is normally to prototype your functionality and performance test it, if this is a concern. My guess is its probably not that bad.
Emmanuel Bernard
author
Ranch Hand

Joined: Oct 22, 2008
Posts: 62
Another approach is to use bulk queries in HQL

insert ... select ...
update ...
delete ...

which will update the data using an SQL query wo having to load the objects.

check the Hibernate reference documentation for more info
Bhavin Sanghani
Ranch Hand

Joined: Dec 17, 2003
Posts: 67
I think, with this approach it would be a problem if you are logging all the actions (INSERT, UPDATE, DELETE) in the database. You may have to write separate logic to log actual transaction..isn't it?
Emmanuel Bernard
author
Ranch Hand

Joined: Oct 22, 2008
Posts: 62
Maybe, it depends what you mean by logging and how you do it. But it's true that object lifecycle is not called.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


You may have to write separate logic to log actual transaction..isn't it?

Do you mean seperate to the database's own transaction log?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate - update specific field with unique id