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: Howto persist a class as a subclass
PostPosted: Mon Apr 24, 2006 9:59 pm 
Beginner
Beginner

Joined: Mon Apr 24, 2006 9:47 pm
Posts: 33
Location: Kansas City, MO
I have a simple class hierarchy.

class Resource;
class Staff extends Resource;
class Coordinator extends Staff;

The corresponding tables are:

RESOURCE
STAFF
COORDINATOR

They join by primary key.

A Resource is a perspective Staff member, and a Staff member could become a Coordinator, but the subclasses all share the same information provided by the superclass, Resource.

My question is this.

Once I persist a Resource object, how do I change that resource object into a Staff or Coordinator object with Hibernate?

Working with Inheritance Objects is not documented extensively in the Hibernate Reference as of version 3.1.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 2:54 am 
Newbie

Joined: Thu Oct 06, 2005 8:08 pm
Posts: 19
Quote:
Once I persist a Resource object, how do I change that resource object into a Staff or Coordinator object with Hibernate?


Can you create the object as a Staff or Coordinator object and then persist it? I don't know how to change the class of an object after it has been created.

Your hbm.xml for Staff would start off with something like:
Code:
    <joined-subclass
             name="com.company.Staff"
             table="Staff"
             extends="com.company.Resource">
        <key column="ID"/>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 10:45 am 
Beginner
Beginner

Joined: Mon Apr 24, 2006 9:47 pm
Posts: 33
Location: Kansas City, MO
I have a method which copies all properties from one Resource to another.

Resource r = (Resource)Session.get(Resource.class, 1); // Get the Resource from Hibernate.
Staff s = new Staff(); // Create a new Staff object.
Resource.copy(r, s); // Copy the properties from the Resource to the Staff object.
Session.evict(r); // You must evict the Resource from the Session or you get "org.hibernate.HibernateException: Found shared references to a collection:" when you try to save, update or merge the resource.

-- To Persist the Change --
Session.merge(s); // Creates a new Resource & Staff record. I need it to update the existing Resource record and insert a new Staff record.
-- OR --
Session.update(s); // Produces a "org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect):"


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.