-->
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: Cannot insert new row with composite-id
PostPosted: Sat Feb 28, 2009 9:12 pm 
Newbie

Joined: Sat Feb 28, 2009 8:52 pm
Posts: 1
Hello everybody,
I've got a table with two foreign keys composite id.

i want to insert or update rows in this table. I'm using the next mapping with hibernate 3.0

Code:
<class name="ClassA" table="cv_axb">
        <composite-id>
           <key-many-to-one name="b" class="ClassB">
              <column name="b" not-null="true"></column>
           </key-many-to-one>
           <key-many-to-one name="c" class="ClassC">
              <column name="c" not-null="true"></column>
           </key-many-to-one>
        </composite-id>
       
        <property name="otherProperty" type="java.lang.Integer">
            <column name="otherProperty"/>
        </property>
    </class>


When I try to insert new rows, I set in "b" and "c" attributes not-null values, but "merge()" method automatically launches a select query before insert:

Code:
select
        cv_axb0_.b as b_1_4_0_,
        cv_axb0_.c as c_4_0_,
        cv_axb0_.otherProperty as otherPro3_4_0_
    from
        cv_axb cv_axb0_
    where
        cv_axb0_.c=?
        and cv_axb0_.b=?


Here it sets b and c to null and then,

Code:
insert
    into
        cv_axb
        (d, c, b)
    values
        (?, ?, ?)


I got a "Could not execute JDBC batch update" Error cause b and c are not nullable, and I don't know how to do it.
I've tried

Code:
select-before-update = "false"


but it doesn't work.

Thnx to all!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 02, 2009 4:30 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Try to use persist() instead of merge() when saving new entities.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 02, 2009 6:31 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
As per the documentation merge should also be working for Transient or new objects. If you try merge for a simple id object it works. May be there is some bug in merge for handling key-many-to-one mapped entities. Your problem should be solved if you use composite-id with identifier class, like:

Code:
    <class name="ClassA" table="cv_axb">
        <composite-id name="id" class="ClassAId">
           <key-property name="bId" column="b" />
           <key-property name="cId" column="c" />
        </composite-id>
        <many-to-one name="b" class="ClassB" insert="false" update="false">
              <column name="b" not-null="true"></column>
        </many-to-one>
        <many-to-one name="c" class="ClassC" insert="false" update="false">
              <column name="c" not-null="true"></column>
        </many-to-one>
        <property name="otherProperty" type="java.lang.Integer">
            <column name="otherProperty"/>
        </property>
    </class>

public class ClassAId implements Serializable{

   long bId;
   long cId;
   //Getter Setter
}

public class ClassA {
   ClassAId id;
   int otherProperty;
   ClassB b;
   ClassC c;
   //Getter Setter
}

_________________
Regards,
Litty Preeth


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.