I have the following mapping file containing a composite key:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="au.net.isa.infobank.server.persistence.hibernate.UserAgencyAsgtHPO" table="user_agency_asgt" schema="public" dynamic-insert="true" dynamic-update="true">
<composite-id>
<key-many-to-one name="user" column="user_id" class="au.net.isa.infobank.server.persistence.hibernate.UserHPO" />
<key-many-to-one name="role" column="role_id" class="au.net.isa.infobank.server.persistence.hibernate.RoleHPO"/>
<key-many-to-one name="agency" column="agency_id" class="au.net.isa.infobank.server.persistence.hibernate.AgencyHPO"/>
</composite-id>
<property name="isPrimaryAgency" type="boolean" not-null="true">
<column name="primary_agency" />
</property>
</class>
</hibernate-mapping>
The User mapping file contains the following SET:
Code:
<set name="userAgencyAsgts" cascade="all,delete-orphan">
<key>
<column name="user_id" not-null="true" />
</key>
<one-to-many class="au.net.isa.infobank.server.persistence.hibernate.UserAgencyAsgtHPO" />
</set>
Role and Agency do not need a set of UserAgencyAsgts.
I have a situation where I am updating an existing User object and adding a single UserAgencyAsgt to it's collection of asgts (which is initially empty). However, when I call SaveOrUpdate I get the following exception:
Code:
08:46:17,931 ERROR [JDBCExceptionReporter] Batch entry 0 update public.user_agency_asgt set user_id=null where user_id=49 was aborted. Call getNextException to see the cause.
08:46:17,931 WARN [JDBCExceptionReporter] SQL Error: 0, SQLState: 23502
08:46:17,931 ERROR [JDBCExceptionReporter] ERROR: null value in column "user_id" violates not-null constraint
08:46:17,931 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)
...
I have debugged this process and all data being given to Hibernate appears to be correct.
Why would Hibernate be using a sql statement of update public.user_agency_asgt set user_id=null where user_id=49? It clearly has the correct user id, so why is it trying to set it to null? In this instance, it shouldn't even be trying to update: it should be adding a new User_Agency_Asgt, as this User does not have any user_agency_asgts to begin with.
I know composite keys are not generally the best way of doing things, and I will probably change this table to have a normal primary key. However, I would still like to know why this is happening, as it just doesn't make sense to me.