• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

update

 
james edwin
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
  • 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...
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
  • 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 !!!
 
Ogi K
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic