I am using mysql and hibernate to insert and update rows in a table. I use saveOrUpdate call. Now, When I try to update a row in a table, I get a exception. Exception states that my column requestTime cannot be null. Obviously its true since I have setup the column property to be NotNull.
I am able to add the row. But when update it with values of 2 more columns, I get this exception.
I am assuming that when I update, I need to read the row from the table, and update the entire row. Is that true ? I was kinda hoping that hibernate saveOrUpdate would do it for me. So I have a Object that has getters for all the columns when I insert a new row. But when I update I have a Object that only has getters for the primary key and new columns.
Transaction txD;
Session session;
session = currentSession();
txD = session.beginTransaction();
session.saveOrUpdate(dataStore);
txD.commit();
Exception
749368 [Thread-2] DEBUG org.hibernate.internal.util.EntityPrinter - com.mcruiseon.carpool.concrete.SubscribeProviderConcrete{acceptedTime=Mon Jul 30 03:39:23 UTC 2012, requestTime=null, subscriberIdentityHash=1218553253, requestIdentity=167093126, subscribedProviderHash=-284086361, isAccepted=true}
749375 [Thread-2] DEBUG org.hibernate.SQL - update carPoolSubscribedProvider set subscriberIdentityHash=?, requestTime=?, subscribedProviderHash=?, isAccepted=?, acceptedTime=? where requestIdentity=?
749398 [Thread-2] DEBUG org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Column 'requestTime' cannot be null [n/a]
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'requestTime' cannot be null
select * from carPoolSubscribedProvider
+-----------------+------------------------+---------------------+------------------------+------------+--------------+
| requestIdentity | subscriberIdentityHash | requestTime | subscribedProviderHash | isAccepted | acceptedTime |
+-----------------+------------------------+---------------------+------------------------+------------+--------------+
| 167093126 | -284086361 | 2012-07-27 16:13:19 | 1218553253 | 0 | NULL |
+-----------------+------------------------+---------------------+------------------------+------------+--------------+
Edit :
| carPoolSubscribedProvider | CREATE TABLE `carPoolSubscribedProvider` (
`requestIdentity` varchar(50) NOT NULL DEFAULT '',
`subscriberIdentityHash` varchar(100) NOT NULL,
`requestTime` datetime NOT NULL,
`subscribedProviderHash` varchar(100) DEFAULT NULL,
`isAccepted` tinyint(1) DEFAULT '0',
`acceptedTime` datetime DEFAULT NULL,
PRIMARY KEY (`requestIdentity`),
Hibernate cannot figure out how you want attribute with null value be treted. It cannot make difference between cases where:
Setting value for attribute that was previously non-null to null means that value of attribute should be changed.
If you want to make column never be part of update statement, you can set updatable attribute to false in @Column annotation. According documentation it defines: Whether the column is included in SQL UPDATE statements generated by the persistence provider