• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

saveOrUpdate method always insert new record..

 
Thillakan Saba
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have a problem with saveorupdate method.

Sample HBM file,



plateTypeCode is nullable othere two not nullable.

numberPlate ='test'
vehicleState = 'tes'
plateTypeCode = nulll

when I execute the saveorupdate method each i its add a new entry to DB.

Hibernate generated SQL




When i set the all three property everything working fine. Problem with setting null to PlateTypeCode.

How can I overcome this problem.

Any input on this.



 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using a nullable field in a key is a bad idea. Remember that in relational databases keys are compared using equality (e.g. key1 = key2) but in SQL null != null so you can't use the same comparison behaviour to check keys if it contains a null. If Hibernate cannot find a record matching the key you pass it will creqate one. If you changes your nullable field to be not null and used an empty String value it should be ok. Or even better, change your datamodel to use a surrogate key and you won't have this problem at all.
 
Thillakan Saba
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul,
we can use is operator for null check. I thought , when the key value is null hibernate sql = operator should changed to is operator.


select plate_.NUMBER_PLATE, plate_.PLATE_TYPE_CODE, plate_.VEHICLE_STATE,
from PLATES plate_
where plate_.NUMBER_PLATE = ?
and plate_.PLATE_TYPE_CODE is ?
and plate_.VEHICLE_STATE = ?


Anyway,I am currently using empty string when it is null.

 
Thillakan Saba
Ranch Hand
Posts: 53
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic