aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate how to update only one colum value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate how to update only one colum value" Watch "Hibernate how to update only one colum value" New topic
Author

Hibernate how to update only one colum value

Asheesh Nehra
Ranch Hand

Joined: Jun 25, 2009
Posts: 35
I need to update only one value in a table through hibernate, the problem is when i try to update only on column in table then i got exception

and if i set all the value of the columns then it works.
Can any aboy help how can i update onlu one value in table row.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Normally, if Hibernate is managing the object and you change one property, the update statement only updates that one field.

Now the error you are getting, I don't think it is because of Hibernate trying to update more fields, it sounds more like either database corruption, like the update statement failed because it updated no records cause it couldn't find a record or more. Or even if it is that it could be the mapping is incorrect for the @Id.

Can you post the mapping for that one class that you are trying to update?

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Asheesh Nehra
Ranch Hand

Joined: Jun 25, 2009
Posts: 35
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

OK, well that is really simple mapping there, and it all looks very good. So I guess that is not the problem.

Now the question is what is the update statement that it is running.

In your session factory configuration you can add the property hibernate.show_sql = true so that the statements are displayed iin your console to see the query. The other thing to figure out is what is the constraint that it is violating? What constraints are on the table besides the PK?

Mark
Rebecca Hopper
Ranch Hand

Joined: Aug 19, 2001
Posts: 38

In the error message, I see DyeDao, and you are updating DyeCirculation? So, I agree with Mark, it is probably the FK to this table which is missing. Maybe you need a one-to-many and many-to-one mapping added to these 2 classes.


SCJP 1.4
Asheesh Nehra
Ranch Hand

Joined: Jun 25, 2009
Posts: 35
Thank you both , for the reply , now the query tells that it tries to update every field generated at console. that is why it generates the constraint violation error as emp_id is FK.
here is query!!!


now how can i update only one column in table ?
Rebecca Hopper
Ranch Hand

Joined: Aug 19, 2001
Posts: 38

Try setting not-null="false" for those values in your table which are not required by the database. I can't remember offhand whether it defaults to true or false, but we always set ours, one way or another.

If that doesn't help, please post the code where you are trying to update. It should be something like this:



Also, if you have a Dye class, and Dye.id = die_id in DyeCirculation class, then you should have a foreign key mapping in your DyeCirculation.hbm.xml, or the database will not save/update/cascade in the way you intend. Also, you will not be using Hibernate to the extent of its performance-enhancing abilities.

Also, try looking here Hibernate Mapping Cheat Sheet
or here Hibernate Common Problems FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate how to update only one colum value