-->
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.  [ 1 post ] 
Author Message
 Post subject: Table per class hierarchy with undeclared subclasses?
PostPosted: Wed Sep 10, 2008 2:31 am 
Newbie

Joined: Fri Aug 29, 2008 1:20 am
Posts: 4
Hi!
I've been trying to do the following, without success until now, and I am wondering whether that is possible at all.
Here is my idea: I want to develop a library, let's say to manage a zoo :o) In that library, I have a class called Animal that can be persisted with hibernate:

@Entity
public class Animal {
@Id String individualName;
@Property int numberOfLegs;
public void roar() { return null; }
}

I want the user of the library to be able to define his own subclasses of Animal and to persist/retrieve his instances of these classes without caring about Hibernate.
The trade-off, of course, is that he will only be able to persist the individualNames and the numberofLegs of his instances, such as defined in the Animal class.
The use case for that is that typically, he won't have any further attributes in his subclasses and will only overload the roar() method.

So I went for the table-per-class-hierarchy solution with a discriminator column in the table that stores the actual class of the object.
So I have a table ANIMALS with the following columns: INDIVIDUAL_NAME, NUMBER_OF_LEGS and ANIMAL_CLASS.
This works fine when I persist instances of Animal.

However, if I define a subclass Lion, I either get a "Unknown Entity" exception when I perform a persist(myLion), or they get stored as an Animal (with the value "Animal" in the ANIMAL_CLASS column) if I use persist("Animal", myLion).
Is there a way to get Hibernate to automatically "recognize" that Lion is actually a subclass of Animal in the entity mappings of the session, without having to explicitly annotate / write a mapping file for all the subclasses?

Thanks,

Nicolas.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.