-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: key-many-to-one mapping violates not null constraint
PostPosted: Thu Jan 12, 2006 7:30 pm 
Newbie

Joined: Thu Nov 24, 2005 6:23 pm
Posts: 7
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 9:43 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
(This is only a theory)

Hibernate may be trying to be clever: it knows, at the time of your saveOrUpdate, that there is exactly one UserAgencyAsgt. Before saving that one object to the set, it is updating the table to remove all objects from the set. That way it can be sure of DB accuracy.

The first thing I'd try doing would be to set inverse="true" on your userAgencyAsgt set, if UserAgencyAsgts are managed outside of the User class. That one has caught me, more than once.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 9:49 pm 
Newbie

Joined: Thu Nov 24, 2005 6:23 pm
Posts: 7
I don't think that would work in my situation, as UserAgencyAsgts are only created/updated when a user is create/updated. Thanks for the suggestion, though.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.