-->
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.  [ 6 posts ] 
Author Message
 Post subject: Unique foreign key
PostPosted: Wed Mar 28, 2007 3:41 am 
Newbie

Joined: Mon May 08, 2006 12:14 am
Posts: 18
Hi,

I have a table with three fields - a primary key, a unique foreign key and another varchar field. I want to use saveOrUpdate() in the corresponding hibernate code. I want to modify the varchar field if the foreign key value already exists. But it is throwing unique constraint violation (org.hibernate.exception.ConstraintViolationException). Can I use saveOrUpdate in such a situation? Otherwise which is the optimal way to do that?

Thanks & Regards
Merin


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 28, 2007 11:08 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi merinshaji,
could you post your mapping files?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 12:44 am 
Newbie

Joined: Mon May 08, 2006 12:14 am
Posts: 18
Hi,

This is my mapping file:


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping default-access="field">
<class name="com.ProcessStatus" table="PROCESS_STATUS">
<id name="id" column="ID"><generator class="native" /></id>
<property name="status" column="STATUS" />
<many-to-one name="processDef" column="PROCESSDEF"
foreign-key="FK_PROCDEF_STATUS" unique="true" />
</class>
</hibernate-mapping>[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 2:07 am 
Regular
Regular

Joined: Thu Dec 22, 2005 7:47 am
Posts: 62
Location: Czech Republic
I was thinking, when you call saveOrUpdate and the constraint is thrown, you do it on a new instance?

1. I think you always have a chance to (a) delete the instance, (b) save a new one (two lines o f code, but also two db operations).

2. i believe that if you in one session load the instance you want to save, change the varchar column and than call saveOrUpdate, you wont have problems with FK or UNIQUE violantions (i do it). would you please send a log file with sql concerning the update? it would seem to me like hibernate is issuing an insert instead of an update.

regards, martin


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 3:16 am 
Newbie

Joined: Mon May 08, 2006 12:14 am
Posts: 18
This is the error I am getting:


Unique constraint violation: SYS_CT_732 in statement [insert into PROCESS_STATUS (ID, STATUS, PROCESSDEF) values (null, ?, ?)]

org.hibernate.exception.ConstraintViolationException: could not insert: [com.ProcessStatus]


It is calling insert, not update.

This is my hibernate code:

ProcessStatus processStatus=new ProcessStatus();
processStatus.setStatus("active");
processStatus.setProcessDef(definition);
Transaction transaction=session.beginTransaction();
try {
session.saveOrUpdate(processStatus);
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
}
transaction.commit();



How will I know if I can load the instance I want to save? I can load it only if the record is present in the table, no? I want to do save for new instance and do update for existing instance using saveOrUpdate. I also don't want to delete the existing record and save again.

Thanks & Regards
Merin


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 4:01 am 
Regular
Regular

Joined: Thu Dec 22, 2005 7:47 am
Posts: 62
Location: Czech Republic
you dont want to craete a new instance (it will always insert), but update an existing one. well, if that's the case i think there is something wrong with your model.

if you have many-to-one with unique constraint, it is in fact one-to-one, so each of your processDef is allowed to have exactly one processStatus instance, am i correct?

if so, you can easilly pick the one you want -- there is only one that matches (you may use hql for example and join ProcessStatus and ProcessDefinition). if you really try to model 1:1 with foreing-key and unique (nothing wrong with that), consider making the association bidirectional

the parent part:

Code:
    <one-to-one name="operation" class="BookEntryOperation" cascade="all"
      property-ref="bookEntry"/>


the child part:

Code:
      <!-- should be uniq, 1:1, some db (MSSQL) have problems -->
      <many-to-one name="bookEntry" class="BookEntry" column="book_entry_id"
   not-null="false" unique="false"/>


just note that i have unique=false in child part, you will have unique true. the problem with my mapping is that is use inheritance and book_entry_id column must be enabled to store NULL values (table-per-hierarchy strategy), in case of MSSQL server for example there are forbuidden multiple NULL values in one unique column (at least i do not know how to enable :-)

once you have it bidirectional, no problem to write the code (and a proper update will be issued -- check it) -- you will be able from processDefinition with single property getter load the proper processStatus.

you can do for example:

Code:
Session session = sessionFactory.getCurrentSession();
Transaction tx = null;
try {
    tx = session.beginTransaction();
    session.lock(processDefinition);
    // i assume there is always instance of processStatus attached to processDefinition
    processDefinition.getProcessStatus().setActive("active");
    session.saveOrUpdate(processDefinition);
    tx.commit();
} catch (HibernateException e) {
    if (tx != null) tx.rollback();
    e.printStackTrace();
    session.close(); // if you keep the sesion opened for current thread untill error
}


was this helpfull?

just an note: i would guess that from a ProcessDefinition there can be several running processes of that type (1:n) independently and each of them should have their state (1:1) (ProcessSatus) -- in that case i miss the 1:n relation.

m.


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