• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Hibernate how to update only one colum value

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Asheesh Nehra
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 38
jQuery Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Asheesh Nehra
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
jQuery Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic