-->
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.  [ 2 posts ] 
Author Message
 Post subject: Unique key involving two columns
PostPosted: Fri Feb 22, 2008 4:27 pm 
Beginner
Beginner

Joined: Thu Jan 13, 2005 10:50 am
Posts: 30
Location: Minneapolis, MN
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:hibernate3.2.1.ga

Mapping documents:
Code:
<hibernate-mapping>
    <class name="com.A" table="TABLE_A">
        <id name="aId" type="java.lang.Long">
            <column name="A_ID" precision="29" scale="0" />
            <generator class="increment" />
        </id>
        ......
   
     </class>
</hibernate-mapping>

<hibernate-mapping>
    <class name="com.B" table="TABLE_B">
        <id name="bId" type="java.lang.Long">
            <column name="B_ID" precision="29" scale="0" />
            <generator class="increment" />
        </id>
        ......
     
     </class>
</hibernate-mapping>
<hibernate-mapping>
    <class name="com.C" table="TABLE_C">
        <id name="cId" type="java.lang.Long">
            <column name="C_ID" precision="29" scale="0" />
            <generator class="increment" />
        </id>
        <many-to-one name="a" class="com.A" fetch="select">
            <column name="A_ID" precision="29" scale="0" not-null="true"  unique-key="aAndBUnique"/>
        </many-to-one>       
        <many-to-one name="b" class="com.B" fetch="select">
            <column name="B_ID" precision="29" scale="0" not-null="true"  unique-key="aAndBUnique"/>
        </many-to-one>       
        ......
     
     </class>

</hibernate-mapping>



NA

ORA-00001: unique constraint (AANDBUnique) violated

Oracle 9

NA

NA


Problems with Session and transaction handling? No

As you can see from the above mapping. The column A_ID and B_ID in class C are composite unique keys. They are primary keys for table A and B respectively.

But when I call
C c = new C();
c.setA(new A(1));
c.setB(new B(2));


saveOrUpdate(c);//Call 1

c.set(....);//update C

saveOrUpdate(c);//Call 2

Call 1 inserts in the value in the database, which is correct. But the Call2 instead of updating the row, tries to insert the row and hence violating the unique key constraint.
How should I map a table with composite unique key constraint?
Code:
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 23, 2008 8:00 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
did you read http://www.hibernate.org/hib_docs/reference/en/html/objectstate.html?
Is this all happening inside a transaction?


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