-->
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.  [ 3 posts ] 
Author Message
 Post subject: Creating record in a table mapped to a joined-subclass
PostPosted: Mon Jan 16, 2006 3:04 am 
Newbie

Joined: Wed Jan 11, 2006 5:21 am
Posts: 14
I’m using hibernate to store two types of User objects. The first one is the generic UserData, and the second one is a ReviewerData (Subclass of UserData with additional attributes).

The hibernate mapping is defined below. I can create both objects regardless whether it’s a UserData or ReviewerData without any problem. However I cannot convert a generic UserData to become ReviewerData. The scenario is that, today a user may be registered as a normal user (e.g UserData), tomorrow, this user may become a reviewer (ReviewerData). Can anyone help me save an existing UserData as a ReviewerData using the following hibernate mapping?

Kind regards,

Roy

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="com.passtheboard.user.model.UserData" table="USER" lazy="false">

                <id name="id" column="id" type="string" length="20"/>               

                <property name="password"       type="string" length="20" not-null="true"/>

                <property name="firstName"      type="string" length="30" not-null="true"/>

                <property name="middleName"     type="string" length="30"/>

                <property name="lastName"       type="string" length="30" not-null="true"/>

                <property name="birthDate"      type="date"/>

                <property name="gender"         type="string" length="1" not-null="true"/>               

                <component

                    name="homeAddress"

                    class="com.baligya.util.model.AddressTransferObject">

                    <property name="street"  column="homeStreet"  type="string" length="100" not-null="true"/>

                    <property name="city"    column="homeCity"    type="string" length="30" not-null="true"/>

                    <property name="state"   column="homeState"   type="string" length="30"/>

                    <property name="zipCode" column="homeZip"     type="string" length="10"/>

                    <property name="country" column="homeCountry" type="string" length="2" not-null="true"/>

                </component>

               

                <component

                    name="workAddress"

                    class="com.baligya.util.model.AddressTransferObject">

                    <property name="street"  column="workStreet"  type="string" length="100" not-null="true"/>

                    <property name="city"    column="workCity"    type="string" length="30" not-null="true"/>

                    <property name="state"   column="workState"   type="string" length="30"/>

                    <property name="zipCode" column="workZip"     type="string" length="10"/>

                    <property name="country" column="workCountry" type="string" length="2" not-null="true"/>

                </component>

               

                <component name="homePhone"

                    class="com.baligya.util.model.TelephoneTransferObject">

                    <property name="countryCode" column="homePhoneCountry" type="string" length="3"/>

                    <property name="areaCode"    column="homePhoneArea"    type="string" length="5"/>

                    <property name="number"      column="homePhoneNumber"  type="string" length="8"/>                   

                </component>

               

                <component name="workPhone"

                    class="com.baligya.util.model.TelephoneTransferObject">

                    <property name="countryCode" type="string" column="workPhoneCountry" length="3"/>

                    <property name="areaCode"    type="string" column="workPhoneArea" length="5"/>

                    <property name="number"      type="string" column="workPhoneNumber" length="8"/>                   

                </component>               

               

                <component name="cellPhone"

                    class="com.baligya.util.model.TelephoneTransferObject">

                    <property name="countryCode" type="string" column="cellPhoneCountry" length="3"/>

                    <property name="areaCode"    type="string" column="cellPhoneArea"    length="5"/>

                    <property name="number"      type="string" column="cellPhoneNumber"  length="8"/>                   

                </component>

               

                <property name="email" type="string" length="50"/>

                <component name="auditData"

                    class="com.baligya.util.model.AuditData">

                    <property name="updateCount" type="int"/>

                    <property name="updateDateTime" type="timestamp"/>

                    <property name="updatedBy" type="string" length="20"/>

                </component>

                               

                <list

                    name="roles"

                    table="userRoles"

                    lazy="false" cascade="all,delete-orphan" fetch="join">

                    <!--

                    <key column="id" not-null="true"/>

                    -->

                    <key not-null="true">

                        <column name="id" unique-key="roleKey"/>

                    </key>

                    <list-index column="roleIndex"/>

                    <composite-element class="com.passtheboard.user.model.UserRole">

                        <property name="name" length="20" unique-key="roleKey"/>

                    </composite-element>

                </list>

                                               

                <joined-subclass name="com.passtheboard.reviewer.model.ReviewerData" table="REVIEWER"   >

                    <key column="id"/>

                    <property name="degreeID"      type="string" length="20"/>

                    <property name="courseID"      type="string" length="20"/>

                    <property name="accountStatus" type="string" length="20"/>

                    <component name="college"

                        class="com.passtheboard.reviewer.model.CollegeData">

                        <property name="collegeName" column="college" type="string" length="60"/>

                        <component

                            name="address"

                            class="com.baligya.util.model.AddressTransferObject">

                            <property name="street"  column="collegeStreet"  type="string" length="100" not-null="true"/>

                            <property name="city"    column="collegeCity"    type="string" length="30" not-null="true"/>

                            <property name="state"   column="collegeState"   type="string" length="30"/>

                            <property name="zipCode" column="collegeZip"     type="string" length="10"/>

                            <property name="country" column="collegeCountry" type="string" length="2" not-null="true"/>

                        </component>

                        <property name="yearStarted"   column="collegeYearStarted" type="int"/>

                        <property name="yearGraduated" column="collegeYearGrad" type="int"/>                       

                    </component>

                    <property name="planCode" type="string" length="10"/>

                    <property name="targetExamDate" column="examDate" type="date"/>                   

                </joined-subclass>

        </class>



</hibernate-mapping>




Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 16, 2006 5:29 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:52 am
Posts: 52
Location: Zurich
Hello Roy,
Quote:
However I cannot convert a generic UserData to become ReviewerData. The scenario is that, today a user may be registered as a normal user (e.g UserData), tomorrow, this user may become a reviewer (ReviewerData).

In these sort of cases, inheritance is probably not the best solution.
Why not model the review data differently?
Ex: with one-to-one relationship.


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

Joined: Mon Apr 24, 2006 9:47 pm
Posts: 33
Location: Kansas City, MO
I solved this problem by writing a conversion method that inserts the superclass's ID into the subclass table. A subsequent call to session.get(Subclass.class, id) returns the Subclass instance as desired. I'm still searching for some built in hibernate operation that does this for you, but I'm coming up with lint.


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