-->
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.  [ 5 posts ] 
Author Message
 Post subject: How to handle unique constraints in DB
PostPosted: Tue Oct 24, 2006 8:38 pm 
Newbie

Joined: Mon Mar 15, 2004 5:48 am
Posts: 9
Hibernate version: 3.2.0
DB: Oracle 9

I have a table with a surrogate key as well as a natural key. The natural key has a uniqieness constraint. I find that when using hibernate I often get a unique constraint error.

My question: is there some way to make hibernate check whether the unique key exists so that rather than doing an insert when I saveOrUpdate it will do a select and use the current surrogate key if a record exists with the unique key?

I have tried all combinations of <natural-id> as well as the unique-key attribute to no avail.

If there is not an automatic way to handle this, what is the best way?

Here is a snippet of mapping document to show what I mean:

Code:
    <class name="myclass" table="atable">
        <id name="grpId" type="java.lang.Long">
            <column name="GRP_ID" precision="10" scale="0" />
          <generator class="sequence">
             <param name="sequence">SEQ_GRP</param>
          </generator>
        </id>
      <natural-id mutable="false">
        <property name="grpNumber" type="string" unique-key="xxx">
            <column name="GRP_NUMBER" length="16" not-null="true" />
        </property>
        <property name="sCode" type="string">
            <column name="S_CODE" length="4" not-null="true" unique-key="xxx"/>
        </property>
        <property name="jCode" type="string">
            <column name="J_CODE" length="4" not-null="true" unique-key="xxx"/>
        </property>
      </natural-id>
...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 24, 2006 10:19 pm 
Beginner
Beginner

Joined: Mon Nov 29, 2004 5:34 pm
Posts: 35
Since you have sequence generator you should not be getting the unique key constraint error.
Add the Select-before-update for your mapping file.

Code:
<class name="myClass" table="myTable" select-before-update="true">

      <id name="myKey" type="java.lang.Long" column="MY_KEY">
         <generator class="sequence">
            <param name="sequence">MY_ADMN.SEQ_KEY</param>
         </generator>
      </id>

      <version name="lockNum" type="java.lang.Long" column="LOCK_NUM" />




My Forte 4GL and Spring Blog

srigold


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 12:27 am 
Newbie

Joined: Mon Mar 15, 2004 5:48 am
Posts: 9
I realise I haven't described my scenario in enough detail. select-before-update doesn't help in my case because I don't have the actual surrogate key value when I am creating it.

This is the scenario:
- I receive some data for the first time, grpNumber=1, sCode=A, jCode=B
- do a saveOrUpdate() -> hibernate does an insert. All is well.
- I receive some data again. The natural key elements are the same, i.e. grpNumber=1, sCode=A, jCode=B
- do a saveOrUpdate() -> hibernate does an insert because the primary key field is null.
-> this results in a "Unique Constraint violated" error from Oracle.

Now, I know that I can do a query first to see if there is a matching enrty and then just do an update. But I was hoping that there was a way to get hibernate to do a select first to check the natural key.

In this example with only a single table it is not a big deal, but I have more complex examples that would involve walking through a variety of child objects to perform the necessary checks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 2:35 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Try custom SQL to merge data (see SQL MERGE statement)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 3:28 am 
Newbie

Joined: Mon Mar 15, 2004 5:48 am
Posts: 9
Maybe I am missing something here, but it seems like using a surrogate key while also having a unique natural key is a fairly normal situation and there should be a straight forward way to handle it?

If I had created a composite key instead of using a surrogate key, hibernate would handle this situation for me. Surely there is a way to get hibernate to do a select on the natural key for me without having to do it manually?


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