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.
|