-->
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.  [ 4 posts ] 
Author Message
 Post subject: Issue deleting nullable column in composite-element (SOLVED)
PostPosted: Wed May 24, 2006 5:17 pm 
Newbie

Joined: Wed May 24, 2006 4:40 pm
Posts: 10
Hello

(Should this be filed as a hibernate bug, or am I just missing a configuration detail?)

Consider the following composite-element (relevant portions):

Code:
<hibernate-mapping>
   <class name="model.util.ContactDetails" table="CONTACTDETAILS">
[... snip ...]
      <set name="phoneSet" table="PHONES" lazy="false" cascade="all" sort="unsorted" outer-join="true">
         <key column="CONTACTDETAILSID">
            </key>
         <composite-element class="model.util.Phone">
            <property name="kind" type="java.lang.String" update="true" insert="true" column="KIND"/>
            <property name="number" type="java.lang.String" update="true" insert="true" column="PHONENUMBER"/>
         </composite-element>
      </set>
   </class>
</hibernate-mapping>


When I update a PHONENUMBER, hibernate generates 2 SQL:
Code:
delete from PHONES where CONTACTDETAILSID=? and KIND=? and PHONENUMBER=?

insert into PHONES (CONTACTDETAILSID, KIND, PHONENUMBER) values (?, ?, ?)


Issue: PHONENUMBER is a nullable column. Before the update, the phone number is NULL, so the sql:
Code:
delete from PHONES where CONTACTDETAILSID=10 and KIND='B' and PHONENUMBER=null
does not delete anything!

The subsequent insert, throws a
Code:
ORA-00001: unique constraint (DEV6.PHONES_PK) violated


The question is: how can I get hibernate to generate
Code:
... where ... PHONENUMBER is null...

instead of
Code:
... where ... PHONENUMBER=null...


Any help / pointers will be highly appreicated.

Hibernate version: 3.1

Name and version of the database you are using: Oracle 10g

Thanks

Ajay


Last edited by ajaygautam on Thu May 25, 2006 3:32 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 6:53 pm 
Regular
Regular

Joined: Mon May 22, 2006 2:30 pm
Posts: 74
How exactly are you performing the update? I find it curious that Hibernate is generating a delete and an add for an update, since a delete/add is not equivalent to an update in the case where the primary key is generated by the database.

I had a similar problem when querying to retrieve a record/object that contained null values, and had to do the following when constructing the query criteria:

if ( A.getCd() == null ) {
code = Restrictions.isNull( CODE );
}
else {
code = Restrictions.eq(CODE, A.getCd() );
}
criteria.add(code);


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 9:27 pm 
Newbie

Joined: Wed May 24, 2006 4:40 pm
Posts: 10
Here is how I update:

Code:
Map phones = contactDetails.getPhones();
            if (phones != null)
            {
                Phone contactBsPhone = (Phone) phones.get(Phone.BUSINESS1);
                if (contactBsPhone != null)
                    contactBsPhone.setNumber(telephone);  // <---->
                else
                    contactDetails.addPhone(new Phone(Phone.BUSINESS1, telephone));
            }



We have a SetToMapAdapter, so, phones.get and setNumber all get manipulated directly in the set.

I searched on google and these forums: seems like it is common practice for hibernate to delete and insert for composite-elements.

Ajay[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 25, 2006 3:31 pm 
Newbie

Joined: Wed May 24, 2006 4:40 pm
Posts: 10
Ok. Found the solution.

As per my mapping, there are no non-nullable columns in PHONES table. This would force hibernate to consider all columns when deleting a row. Setting atleast one column to not-null will ignore the nullable columns in the mapping.

Here is my new mapping:
Code:
      <set name="phoneSet" table="PHONES" lazy="false" cascade="all" sort="unsorted" outer-join="true">
         <key column="CONTACTDETAILSID">
            </key>
         <composite-element class="com.kiodex.model.util.Phone">
            <property name="kind" type="java.lang.String" update="true" insert="true" not-null="true" column="KIND"/>
            <property name="number" type="java.lang.String" update="true" insert="true" column="PHONENUMBER"/>
         </composite-element>
      </set>


This generates the delete sql as:
Code:
delete from PHONES where CONTACTDETAILSID=? and KIND=?

Which works fine for me.

Ajay


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.