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.  [ 2 posts ] 
Author Message
 Post subject: Inheritance mapping
PostPosted: Tue Feb 09, 2010 1:46 pm 
Newbie

Joined: Thu Oct 22, 2009 7:15 am
Posts: 3
Consider the following situation: we have two entities with a common superclass:

public abstract class Parent
{
private String name;

public String getName()
{
return this.name;
}

public void setName(String name)
{
this.name = name;
}
}

public class FirstAncestor extends Parent
{
//some string property with getter and setter
}

public class SecondAncestor extends Parent
{
//this class have no it's own properties, only the parent's ones. IMPORTANT: ID of that class should be mapped to "parent" table (see below).
}

And a following DB structure, schematically:

parentsTable(parent_id(primary key), name)
firstAncestorsTable(first_ancestor_id(primary key), parent_id(foreign key references to parentsTable.parent_id), [some other fields])

I'd like to be FirstAncestor class to be mapped to firstAncestors table, FirstAncestor id should be mapped to first_ancestor_id;
SecondAncestor shouln't be mapped to a table directly, but it should take it's ID from parentsTable and it's other fields too. Is it possible to do such a thing? Sorry for my english, it's a foreign language to me.


Top
 Profile  
 
 Post subject: Re: Inheritance mapping
PostPosted: Wed Feb 10, 2010 8:50 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
What you want is a single-table inheritance structure.

You can take something like this:
Image

And map it to a single table like this:

Image

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


Here's a little tutorial on the subject:

http://jpa.ezhibernate.com/Javacode/lea ... ncewithjpa

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