-->
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: Convert subclass in a superclass??
PostPosted: Wed Apr 20, 2005 10:56 am 
Newbie

Joined: Tue Mar 01, 2005 5:00 pm
Posts: 11
Hello,

I implement "Table per class hierarchy".
If I have in my table one row that describes a "MUERTO" item and I would like to convert him to "EJEMPLAR" without change his "Id"...

How can I make it?

I've tried:

Ejemplar e = ...load(Muerto...., id); ----------->error

Muerto m = ...load(Muerto...., id);
Ejemplar e = (Ejemplar) m; ------------>error

if I load MUERTO, make instance of Ejemplar, copy all atributes, delete MUERTO and save Ejemplar, the Id changes because it is assigned automaticly by the database.

Hibernate version: 3

Mapping documents:

Code:
<?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>

<class name="org.modelo.Ejemplar" table="EJEMPLAR" discriminator-value="E">

   <id name="Id" type="java.lang.Integer">
       <column name="ID" not-null="true" unique="true" sql-type="INTEGER" />
       <generator class="identity" />
    </id>
   
   <discriminator column="type" type="character"/>
   
    <many-to-one name="Criador" class="org.modelo.Persona">
        <column name="CRIADOR" not-null="false" />
   </many-to-one>

    <many-to-one name="Propietario" class="org.modelo.Persona">
        <column name="PROPIETARIO" not-null="false" />
   </many-to-one>

    <many-to-one name="Raza" class="org.modelo.Raza">
        <column name="RAZA" not-null="true" />
   </many-to-one>

(...)

   <subclass name="org.modelo.Muerto" discriminator-value="M">

       <property name="Fechadef" type="java.lang.String">
           <column name="FECHADEF" not-null="true" sql-type="VARCHAR" />
      </property>
   
       <property name="Motivo" type="java.lang.String">
           <column name="MOTIVO" not-null="false" sql-type="VARCHAR" />
      </property>
      
   </subclass>
         
</class>

</hibernate-mapping>


Thank you very much.


Top
 Profile  
 
 Post subject: I have the same question - how to upgrade a superclass
PostPosted: Mon Jun 13, 2005 11:30 am 
Beginner
Beginner

Joined: Fri Feb 11, 2005 2:40 pm
Posts: 27
We have USER and EMPLOYEE that extends USER. Initially everyone is a USER and at some point they become EMPLOYEE (that's just an example). When they are employee, there are additional columns to persist.

Right now the USER and EMPLOYEE are mapped to the same table with EMPLOYEE being a <subclass>. The question is, how do we convert a USER to EMPLOYEE using Hibernate?

Of course we don't want to delete a row and insert a new one. There are also associations via foreign keys.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 13, 2005 3:34 pm 
Beginner
Beginner

Joined: Sun Feb 08, 2004 4:09 pm
Posts: 46
This has been discussed a lot. Actually an object shouldn't have it's type changed. If this happens you get ANOTHER object. As there's an underlying relational database that identifies "objects" with and id, someone may get this wrong idea. The solution for this is in design level, you should look for designs (search design patterns) that let's change an objetct's behaviors and properties at runtime.

Imagine a situation that you don't use hibernate as your persistence layer but another mechanism that uses pure oo pesistence, if you want to "change an objetc's type" you'll need to construct another object and navigate through all your domain graph to change for the new reference (what a hell).

Cya!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 8:33 pm 
Beginner
Beginner

Joined: Mon Apr 24, 2006 9:47 pm
Posts: 33
Location: Kansas City, MO
The only way I know to accomplish this is to create a java.sql.PreparedStatement which will change your descriminator column value. Then call session.get(Subclass.class, ID), which will return your object as the desired type.


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.