-->
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: Inheritance strategy 'joined' + discriminator field 'type'?
PostPosted: Mon Oct 03, 2011 11:26 am 
Newbie

Joined: Mon Oct 03, 2011 10:58 am
Posts: 1
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


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.