-->
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.  [ 7 posts ] 
Author Message
 Post subject: Mapping inheritance without discriminator column?
PostPosted: Wed Jul 23, 2008 6:28 am 
Newbie

Joined: Wed Apr 02, 2008 9:05 am
Posts: 8
Hi guys,

I have a question concerning mapping inheritance in Hibernate. I like to map the last subclass of the inheritance map to database tables and I want Hibernate not to use a discriminator column for mapping.

Let's say I have the classes A, B and C and B is subclass of A and C is subclass of B:

C extends B (extends A)

I only like to store "objects" of C in the database. B and A will not be used so that C (as leaf of the tree) is the entity class only used in the application.

This is just an example. I have to deal with various dynamic domain model structures whose depth is unknown.

Is there any possibility to tell Hibernate to avoid the use of the discriminator column? In other words: I have different inheritance structures and I only want to store the last leaf of the inheritance chain (no tree).

Does anyone have an idea?

Thanks for your support.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 24, 2008 9:03 am 
Newbie

Joined: Wed Apr 02, 2008 9:05 am
Posts: 8
any idea?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 3:58 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Would a table per class inheritance mapping do the trick?

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


Image

Quote:
Using the TABLE_PER_CLASS inheritance strategy, Hibernate expects three separate tables to be defined in the database, with each table containing the aggregate of all of the declared and inherited properties of the associated class. As a result, the ancestor table would only have two properties, namely the id and nationality fields, whereas the child table duplicates these properties, adds in columns for the Parent's lastName property, and then adds fields for the locally declared firstName property.


Mapping Inheritance with Hibernate: Inheritance Mapping Strategies:

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=16mappinginheritancewithjpa

_________________
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: Mon Jul 28, 2008 7:15 am 
Newbie

Joined: Wed Apr 02, 2008 9:05 am
Posts: 8
Thanks for your reply. Unfortunately it does not fit to my problem.
I want Hibernate to store only the last child of the inheritance chain. In relation to your example, I only want to store the parent class with the inherited attributes of the ancestor.
So I have only one subclass per class, so that I do not have a inheritance tree, but only a chain and I just want to store the last child (or the "newest" extension) of the chain. In the end there have to be one table in the database representing the parent-class or any other subclass of it.

Does anyone have another suggestion?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 28, 2008 9:14 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
Annotate classes A and B with @MappedSuperclass
and C with @Entity.

This should do the trick.

enjoy

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 11:28 am 
Newbie

Joined: Wed Apr 02, 2008 9:05 am
Posts: 8
That's a good hint. Thank you.
One more question: How can I achieve this is in a dynamic way via Hibernate API? The amount of the subclasses will only be known at runtime. The subclasses will be loaded in a sequence and the last one should be the one to store in the database.

So there can be a "initial" entity class A and maybe some more like B, C and so on.

So we have the inheritance chain C -> B -> A. In each step, the "@entity" annotation(property) of the actual last subclass should be replaced by a "@mappedSuperclass" annotation/property. As the result, the subclass of the last iteration has to be the entity-class, all others of the chain should be marked as "mappedsuperclass".


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 2:30 pm 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
RabbitHunter wrote:
That's a good hint. Thank you.
One more question: How can I achieve this is in a dynamic way via Hibernate API? The amount of the subclasses will only be known at runtime. The subclasses will be loaded in a sequence and the last one should be the one to store in the database.

So there can be a "initial" entity class A and maybe some more like B, C and so on.

So we have the inheritance chain C -> B -> A. In each step, the "@entity" annotation(property) of the actual last subclass should be replaced by a "@mappedSuperclass" annotation/property. As the result, the subclass of the last iteration has to be the entity-class, all others of the chain should be marked as "mappedsuperclass".



Everything you do to that effect must happen before you create your SessionFactory. Of course you might start with a SessionFactory, and later create a new one, but you won't be able to mix instances from sessions originating from different SessionFactorys (at least that is my understanding of the docs).

You can manipulate the Configuration Instance you typically create in HibernateUtil in order to create your SessionFactory from it.

I only added Classes and additional configuration files so I am not so sure about changing the Annotation. But I guess you could build your own Classloader which checks some conditions, and replaces Annotations before loading the class.

kind regards
Jens

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


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