-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problem with strategies for mapping inheritance
PostPosted: Mon Jul 21, 2008 1:09 pm 
Beginner
Beginner

Joined: Mon Nov 19, 2007 4:40 pm
Posts: 46
Hibernate version:3.2

Name and version of the database you are using:Oracle 10g

I have been playing around with the different inheritance mapping strategies outlined in the "Java Persistence with Hibernate" book by Christian Baur and Gavin King, but I am still a little confused about the best strategy for my particular situation. I have tried searching the forum, but can't seem to find a good issue to latch on to.

If have 3 classes:

Code:
public class Person
{
   set/getName();
}

public class Employee extends Person
{
   set/getEmployer();
}

public class User extends Person
{
   set/getRole();
}


These are (obviously) severely simplified examples of my actual classes. In my system, an instance of Person can be created (not exist as an Employee or a User) and that Person can be a User, an Employee, or all three (every User and Employee is a Person).


Requirements:
1. I need to be able to insert a Person object and later "upgrade" that Person to a User, an Employee, or both. To further clarify, I need the object to be a Person, Employee, and User at the same time (e.g. if i query for Person, i get Person record; if i query for Employee, i get the Employee record; if i query for User, I get the User record). I also need to be able to "downgrade" them to just a Person. I was having problems with this when employing the table/subclass strategy.
2. When querying for Person, I want 1 row if the Person is a User AND and Employee (the 1 row should be just Person)... basically, when querying Person, I am content to only receive Person objects (even if that Person is just an Employee and not a User).
3. When updating the data for an Employee, I want the Person data to be updated for Person and User (this makes me think table/subclass is best... but, I think number 1 fails). And, of course, changes in User or Person are reflected in the other 2 objects as well.

I appreciate any advice you can provide. If any clarification is needed, please let me know.

Thanks,

Paul


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 24, 2008 4:42 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 8:18 am
Posts: 31
Read this blog. I hope it will give you answers of many questions:
http://javainnovations.blogspot.com/2008/07/inheritance-strategy-class-per.html

_________________
amer sohail


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 4:11 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
This Person/Employee/User scenario is very similar to the Child-Parent-Ancestor relationship I detail in the Hibernate Mapping section of my website.

Mapping Inheritance with Hibernate: Inheritance Strategies

Personally, I'd go with a single table inheritance strategy. For me, those are most flexible:

Code:
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class Ancestor { }



Quote:
Benefits of the Single Table Inheritance Type

The single table inheritance type is often the best choice for performing inheritance mapping. First of all, it minimizes the number of tables that need to be queried, which can be significant for large class hierarchies. Furthermore, mapping associations can be very difficult with other strategies.

Image

For example, imagine we were using a single class per table strategy, and we decided an Ancestor could be associated in a one to one manner with another type, say a Location type. Since this relationship would be true for all subclasses, and given the fact that associations are implemented at the database level by foreign keys, we would have to add a new foreign key column to every table that was based on a sub-class of Ancestor. In a large class hierarchy, adding those foreign key fields would be extremely onerous and error prone. On the other hand, with the single table inheritance strategy, only one column would need to be added to one table, which would be a much, much simpler process.



Ineritance Mapping

Image

Hibernate and Inheritance Mapping

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 7:44 am 
Beginner
Beginner

Joined: Mon Nov 19, 2007 4:40 pm
Posts: 46
Thanks so much for the pointers... I need to read/digest/play with all this and will post if I have any further issues.

Thanks again for taking the time to post.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 28, 2008 1:49 pm 
Beginner
Beginner

Joined: Mon Nov 19, 2007 4:40 pm
Posts: 46
Ok... So, i have had a chance to read/play with this. I have implemented my hierarchy using the same table for Employee/Person and a different table (joined subclass) for User in order to reduce the number of joins... the typical use is to just get Employees.

Everything works from a query perspective. My question is... and this is not isolated to my implementation as it applies to any of them really... is how can I "upgrade" the object. So, if I insert a Person and then later need to "upgrade" that Person to an Employee, how can I do this within hibernate? Furthermore, how can I downgrade the employee to be just a Person? This downgrade may leave the Person as a User or, if the Person wasn't a User, leave the instance as JUST a Person.

Any thoughts?

Thanks,


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