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: how to update polymorphic one-to-one?
PostPosted: Wed May 25, 2005 10:25 am 
Beginner
Beginner

Joined: Tue Apr 05, 2005 12:09 pm
Posts: 48
Location: Slovakia (SK), Košice (KE)
Hello

I am using hibernate 3.0 and have problem with one-to-one polymorphic property uptade.
I am working with detached objects.

Code:
<hibernate-mapping>
   <class name="com.kovine.kfe.dao.TableColumn" table="KFE_TABLE_COLUMN">
      <id name="id" unsaved-value="null" column="TABLE_COLUMN_ID">
         <generator class="sequence">
            <param name="sequence">KFE_TABLE_COLUMN_ID_SEQ</param>
         </generator>
      </id>

...

      <one-to-one name="inputType" class="com.kovine.kfe.dao.InputType" cascade="all" />
   </class>

   <class name="com.kovine.kfe.dao.InputType" abstract="true">
      <id name="id" column="TABLE_COLUMN_ID">
           <generator class="foreign">
               <param name="property">tableColumn</param>
           </generator>
       </id>
       
       <one-to-one name="tableColumn" class="com.kovine.kfe.dao.TableColumn" constrained="true" />
      
      <union-subclass name="com.kovine.kfe.dao.InputTypeText" table="KFE_INPTYPE_INPUTTEXT">
         <property name="width" column="WIDTH"></property>
      </union-subclass>

      <union-subclass name="com.kovine.kfe.dao.InputTypeTextarea" table="KFE_INPTYPE_INPUTTEXTAREA">
         <property name="width" column="WIDTH" not-null="true" />
         <property name="height" column="HEIGHT" not-null="true" />
      </union-subclass>
   </class>

</hibernate-mapping>



Code:
InputType inputType = new TextareaInputType();

... set inputType  properties ...

// update
Transaction tx = session.beginTransaction();
InputType old = tableColumn.getInputType();
tableColumn.setInputType(null);
session.delete(old);
inputType.setTableColumn(tableColumn);
tableColumn.setInputType(inputType);
session.update(tableColumn);
tx.commit();


This code throws exception:
Code:
org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [com.kovine.kfe.dao.InputTypeText#251]

But I think this message is wrong, because there is a new object inputType which should replace old one. Maybe this message was caused because hibernate only compare ids of related object and if they are the same it consider those object as same too. If I am right, then it could be a bug in hibernate.


Using this fragment of code:
Code:
InputType inputType = new TextareaInputType();

... set inputType  properties ...

// update
Transaction tx = session.beginTransaction();
inputType.setTableColumn(tableColumn);
tableColumn.setInputType(inputType);
session.update(tableColumn);
tx.commit();


causes duplicate entry in inputType tables (the old entry doesn't delete).

Please help me to solve this problem. I was trying many other solutions but none has worked.

Thanks in advance!
Martin


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 2:23 pm 
Beginner
Beginner

Joined: Tue Apr 05, 2005 12:09 pm
Posts: 48
Location: Slovakia (SK), Košice (KE)
I think I found a bug (or undocumented feature ;-) ). This code works as I wanted:
Code:
InputType inputType = new TextareaInputType();

... set inputType  properties ...

// update
Transaction tx = session.beginTransaction();
InputType old = tableColumn.getInputType(); // tableColumn is detached existing instance
tableColumn.setInputType(null);
session.delete(old);
session.flush(); // it must be called for proper function
inputType.setTableColumn(tableColumn);
tableColumn.setInputType(inputType);
session.update(tableColumn);
tx.commit();


I just added the session.flush() command. Is it OK or bug in hibernate? Without flushing hibernate would think that the new instance of InputType is the same as the deleted causing the exception described in previous post. Note that tableColumn is detached existing instance.

Please look at it
Thanks in advance for your effort


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 2:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This is expected.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 4:22 pm 
Beginner
Beginner

Joined: Tue Apr 05, 2005 12:09 pm
Posts: 48
Location: Slovakia (SK), Košice (KE)
Thanks for reply

But is that behaviour described somewhere in the documentation? Or can you (or anybody) explain me in short why is this flushing necessary?

Martin


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.