-->
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.  [ 2 posts ] 
Author Message
 Post subject: Inheritance and Polymorphism problem
PostPosted: Mon Oct 20, 2008 1:54 pm 
Newbie

Joined: Mon Oct 20, 2008 1:50 pm
Posts: 7
Hibernate version: 3


Hi,

I'm new to Hibernate and have a question around inheritance.

I've a base Java abstract class called User. Three classes extend from this but don't contain any additional data themselves, just different methods.

All 3 are stored in a table in the database called User.

Is it possible to create one mapping file for User to cover all 3 sub-classes?

If so, what do I need to do then in my code to get this to work?

Thanks.


Top
 Profile  
 
 Post subject: Re: Inheritance and Polymorphism problem
PostPosted: Mon Oct 20, 2008 4:36 pm 
Newbie

Joined: Thu Oct 09, 2008 4:03 pm
Posts: 16
Location: US, Itasca Illinois
I've not done this, but a few days ago I came across something that sounds similar to your requirements. In this example, they have 1 database table and multiple descendents use it.

http://www.hibernate.org/hib_docs/v3/re ... tance.html

9.1.7. Mixing implicit polymorphism with other inheritance mappings
There is one further thing to notice about this mapping. Since the subclasses are each mapped in their own <class> element (and since Payment is just an interface), each of the subclasses could easily be part of another inheritance hierarchy! (And you can still use polymorphic queries against the Payment interface.)

<class name="CreditCardPayment" table="CREDIT_PAYMENT">
<id name="id" type="long" column="CREDIT_PAYMENT_ID">
<generator class="native"/>
</id>
<discriminator column="CREDIT_CARD" type="string"/>
<property name="amount" column="CREDIT_AMOUNT"/>
...
<subclass name="MasterCardPayment" discriminator-value="MDC"/>
<subclass name="VisaPayment" discriminator-value="VISA"/>
</class>


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