我正在使用mysql和hibernate插入和更新表中的行。我使用saveOrUpdate調用。現在,當我嘗試更新表中的一行時,我收到一個異常。異常狀態表明我的列requestTime不能爲空。顯然它是真正的,因爲我已經設置列屬性爲NotNull。休眠saveOrUpdate,更新需要原始行的所有字段
我可以添加該行。但是,當更新2列的值時,我會得到這個異常。
我假設當我更新時,我需要讀取表中的行,並更新整個行。真的嗎 ?我有點希望,休眠saveOrUpdate會爲我做。所以我有一個Object,當我插入一個新行時,所有列都有getter。但是當我更新時,我有一個只有主鍵和新列的getter的對象。
Transaction txD;
Session session;
session = currentSession();
txD = session.beginTransaction();
session.saveOrUpdate(dataStore);
txD.commit();
異常
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 |
+-----------------+------------------------+---------------------+------------------------+------------+--------------+
編輯:
| 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`),
我是否需要有代碼更新VS插入不同的流量?像if(update){row = getColumn(primaryKey),appendValues(row,newValues)} else else insert? – Siddharth 2012-07-30 11:47:38