• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

update

 
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an Table in which i have 5 fields which are not null(all 5 fields).One of the field is "DEL_I char(1) ".This is field is having value of either "Y" or "N".It's basically a flag.

Now problem during update,i just want to update this field based on primary key.If i pass the object to session.update(email) method,then this method expects all values which are not null in hbm.xml and database to be set in object email.But what my requirement is to only set primarykey of object and this artibute (DEL_I = N).

how to do this...
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


But what my requirement is to only set primarykey of object and this artibute (DEL_I = N).


It stands to reason that if your database constraints don't allow nulls, you can't try to update a record with nulls. Your question seems to be "how do I get my application to ignore database constraints"? To which the answer is: you can't (and you should even really be trying).
 
james edwin
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

Thanks for reply.But DB Constraints should not create any problem while updating.It should create problem while inserting and not updating.When i can run "UPDATE <TABLE NAME> set DEL_I='Y' where PK='XYZ' " from SQL prompt.Then it must be possible through the Hibernate also...

what do u say ?..
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right in SQL terms. However Hibernate doesn't generate SQL that way. Think about it - how do you signal to Hibernate that a property in a POJO which is set to null is set that way because you don't want it to be included in the update statement? Basically in Hibernate if you update one field, you actually update them all.

Assuming you have properly loading the object's properties, you should never have this problem. If you are trying to update an object which doesn't have all the non-nullable properties set you'll get problems.

There are two ways you could be doing that: first you have deliberately (or accidentally) set some property on the object to null. Alternatively you would get this if you are updating an object you didn't actually get from the database, unless you have populated all the non-nullable properties yourself.
 
james edwin
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul.Let me see what to do in hibernate or may be some work around for this i need to find out.

Thanks for help !!!
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
james, in order to do an update in the object world, you must first get a handle of the object you want to update! -- this is a fundamental difference between the object and the relational word. So, the definition of update in the object world is

1. get a handle of the object you want updated -- i.e. READ IT from DB.
2. change any or all values on it ( excluding primary key..if you change primary key, then you must do an insert ).
3. and then save the object back to the DB, i.e. call 'update' on Session interfacce.

You're probably thinking that this less efficent then doing a straight update, like you can in sql..this is true, but in most use cases, you're only updating objects you have already read..otherwise, you wouldn't know what to update...for other cases, including mass-updates, use JDBC.
 
reply
    Bookmark Topic Watch Topic
  • New Topic