简体   繁体   中英

org.hibernate.exception.ConstraintViolationException: could not update - Auto-increment column in DB is NULL

I am upgrading my System's hibernate3 package from version "3.1.1" to "3.6.10".

As I already migrated it to the 3.6.10 version, I am seeing an exception:

[6/12/17 9:54:57:365 SGT] 000000a2 SystemOut     O [DEBUG] [8adbbb8205c9a02b50015c09a02d15e00000|TKN_ENQ_REQ|10.91.48.113] 12/06/17 09:54 ib.CustomerVerification  - [Ex
ception Caught Caused By:]
<b>org.hibernate.exception.ConstraintViolationException: could not update: </b>[com.dbs.tms.main.pojo.Customer#8adbbb825c9a02b5015c9a02da3e0001]
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
        at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2613)
        at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2495)
        at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2822)
        at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:113)
        at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
        at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
        at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:64)
       ... 
       ....
       ...
       ..
<b>Caused by: com.ibm.db2.jcc.am.SqlIntegrityConstraintViolationException: Assignment of a NULL value to a NOT NULL column "TBSPACEID=5, TABLEID=9, COLNO=1" is not allowed
.. SQLCODE=-407, SQLSTATE=23502, DRIVER=3.64.133 </b>
        at com.ibm.db2.jcc.am.bd.a(bd.java:675)
        at com.ibm.db2.jcc.am.bd.a(bd.java:60)
        at com.ibm.db2.jcc.am.bd.a(bd.java:127)

It seems like the column (COL=1) in the CUSTOMERS table (TABLEID=9), when I update is NULL. the value is auto-generated by the Database (DB2) and should be set in the Customer Object.

Column DB Properties:

在此处输入图片说明

Customer's HBM.xml (Highlighted is the column experiencing Null values (should not be Null):

在此处输入图片说明

Hope someone could help to resolve my concern.

I found the solution to my concern.

I added the insert="false" and update="false" in the Customer's HBM.xml.

In the hibernate 3.1.1 version, the two parameters, even if it's not present, the SQL generated is it is not including the tknSysGenId but in 3.6.10 version, it is being included and since the value of it is null, it throws an error upon updating the table in the Database.So we need to explicitly state that the column tknSysGenId should not be included in the Update statement as one of the Parameters.

So the new value of it is:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM