Hello,
I've got a pretty basic question. Let's say I have a class hierarchy A <- B (class B extends class A). Both are beans and saved by hibernate into the database. To map the hierarchy, I know two methods so far: 1. all in same table: A and B will be saved in one table, and the field "type" will identify whether the entry is from type A or B. 2. one table for each type: When a B object is saved, Hibernate will put it in the B table and will also create an entry in the A table. The IDs will be the same to make sure it's one object.
Now I would like to combine these two: Whenever I save an object from type B, it should be written into the B table as well as into the A table. But there should also be a field "type" in the A table, where the type of each entry is shown.
I've tried it with the following annotations in the class A:
@Inheritance(strategy = InheritanceType.JOINED) @DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
The first one defines that each object will be saved into seperate tables, and the second one should write the type into the type-Field.
Class B has this annotation: @DiscriminatorValue("B")
But this unfortunately doesen't work. When I want to insert an A object into the A table, it saves this entry two times and then gives an error that the key is already used. So I guess this isn't the right way to do it. Can anybode help me with that? The tutorials I've found are rather confusing to me, since I'm very new to this topic. Besides, most explanations use xml Files instead of annos :(
Thanks for reading and I would appreciate your help very much :)
greetings, Phil
|