-->
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: not-null constraint violation when deleting an object
PostPosted: Wed Jun 01, 2005 8:34 pm 
Newbie

Joined: Wed Jun 01, 2005 7:51 pm
Posts: 13
When an instance of the object shown below is deleted, Hibernate
attempts to update the ts_recording_id field with null -- despite the
not-null="true" in the <key> definition -- and this fails due to database
constraints. I tried changing:

Code:
<key>
    <column name="ts_recording_id" not-null="true" />
</key>


to:

Code:
<key column="ts_recording_id" not-null="true">


but, this generates a "repeated column mapping in entity" exception.

1. How do I get Hibernate to refrain from setting this column to null?
2. Why aren't the two <key> definitions equivalent?

Hibernate version: 3

Mapping documents:
Code:
<hibernate-mapping>
<class
    name="com.timestock.tess.objects.RecordingSession"
    table="ts_recording_sessions"
    schema="dbo"
    catalog="timestock"
>
    <id
        name="id"
        type="java.lang.Long"
    >
        <column name="ts_id"  not-null="true" unique="true" sql-type="bigint" />
        <generator class="com.timestock.tess.util.IncrementGenerator" />
    </id>
    <version name="version" column="version_info" type="long" />
   
    <property
        name="startDate"
        type="java.util.Date"
    >
        <column name="ts_start_time" scale="23" precision="3" not-null="false" sql-type="datetime" />
    </property>

    <property
        name="stopDate"
        type="java.util.Date"
    >
        <column name="ts_stop_time" scale="23" precision="3" not-null="false" sql-type="datetime" />
    </property>

    <property
        name="saveDate"
        type="java.util.Date"
    >
        <column name="ts_save_time" scale="23" precision="3" not-null="false" sql-type="datetime" />
    </property>

    <property
        name="clientIpAddress"
        type="java.lang.Long"
    >
        <column name="ts_client_ip_address"  not-null="false" sql-type="bigint" />
    </property>

    <property
        name="status"
        type="java.lang.String"
    >
        <column name="ts_status" length="200" not-null="false" sql-type="nvarchar(200)" />
    </property>

    <property
        name="sessionName"
        type="java.lang.String"
    >
        <column name="ts_session_name" length="200" not-null="true" sql-type="nvarchar(200)" />
    </property>

    <property
        name="recorderName"
        type="java.lang.String"
    >
        <column name="ts_recorder_name" length="200" not-null="true" sql-type="nvarchar(200)" />
    </property>

    <property
        name="author"
        type="java.lang.String"
    >
        <column name="ts_author" length="200" not-null="true" sql-type="nvarchar(200)" />
    </property>

    <property
        name="softDelete"
        type="java.lang.Boolean"
    >
        <column name="ts_soft_delete"  not-null="true" sql-type="smallint" />
    </property>

    <set lazy="true"
        name="setOfRecordingComponent"
    >
        <key>
            <column name="ts_recording_id" not-null="true" />
        </key>
        <one-to-many
             class="com.timestock.tess.objects.RecordingComponent"
        />
    </set>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
I am using Spring, so the delete call looks like:
Code:
recordingSessionDao.delete(id);


Full stack trace of any exception that occurs:
can be supplied if needed

Name and version of the database you are using:
MS SQL

The generated SQL (show_sql=true):
update ts_recording_components set ts_recording_id=null where ts_id=?

Debug level Hibernate log excerpt:
can be supplied if needed

[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 01, 2005 8:47 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Stating the obvious but when you deleted the parent you had deleted all of the children?

I suggest you re-read the parent-child relationship and look at what inverse=true means and cascade options.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 02, 2005 2:03 pm 
Newbie

Joined: Wed Jun 01, 2005 7:51 pm
Posts: 13
Thanks for your reply; however, I think that you missed the point.
I did read the doc. Here are 3 excepts from the 3.0.5 doc:

From section 7.2.5:
Quote:
Very important note: If the foreign key column of a <one-to-many>
association is declared NOT NULL, you must declare the <key> mapping
not-null="true" or use a bidirectional association with the
collection mapping marked inverse="true". See the discussion of
bidirectional associations later in this chapter.

From section 6.1.19:
Quote:
The not-null and update attributes are useful when mapping a
unidirectional one to many association. If you map a unidirectional
one to many to a non-nullable foreign key, you must declare the key
column using <key not-null="true">.

This is a unidirectional mapping, but the not-null="true" option works only
if I replace the <key></key> section with <key not-null="true">.
I discovered this by trail and error. The doc states that the column attribute is optional NOT that you can't use it in certain unspecified cases.
In addition, I have several other mappings that look the same except for the column/table names, yet substituting <key not-null="true"> for some
of them generates a duplicate property exception for properties with names like "_setOfAppDefBackref". I can find nothing in the doc that explains when to use the column attribute and when not to use it.

From section 6.1.20:
Quote:
Any mapping element which accepts a column attribute will alternatively accept a <column> subelement

This implies, to me, that
Code:
<key column="col1" not-null="true"/>

and
Code:
<key>
  <column name="col1" not-null="true"/>
</key>

are equivalent; yet, in my case, the 1st generates an exception and the 2nd doesn't work, i.e. doesn't stop Hibernate from generating an update statement that attempts to set the key field to null when an instance
of the class shown is deleted.

Perhaps there is subsequent documentation that explains these behaviors?


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.