-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to Map Inherited Classes
PostPosted: Mon Aug 06, 2007 9:55 am 
Newbie

Joined: Mon Aug 06, 2007 9:29 am
Posts: 3
Hi, maybe someone can give me a little jumpstart on the following problem:

What i want to do is to build a base class with a set of fields and methods, that will be used in 3 subclasses. I try to describe the layout as best I can.

Base Class Contact Contains Tables
ContactID
Addresses
Phones
Faxes
Bank Info
CreditCard Info

Subclass CustomerContact extends Contact and contains additional Tables
CustomerAccount
Personal Info

Subclass BusinessContact extends Contact and contains additional Tables
BusinessAccount
Business Info

Subclass ClientContact extends Contact and contains additional Tables
ClientAccount
Business Info

How can I map this Construct using annotations or xml? I run into the following problems:
Though Hibernate is able to map inherited classes, it needs an identifier in the record of its base table (in this case contactid) to identify the class that this record belongs to. However the same ContactID Record can belong to all 3 subclasses and distuinguishes only by the existence of the tables CustomerAccount, BusinessAccount and ClientAccount which Type i will actually use. Obviously I can model all 3 Objects to be independent from each other and i will be able to map them properly. But this is ugly and not good code. If possible I would like to use the approach via inheritance. Any Comments greatly appreciated.

Regards
Andreas


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 06, 2007 10:20 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
With "Table per class hierarchy" inheritence mapping the discriminator in the base class mapping is something hibernate manages. It doesn't need to appear in your object at all, you never set it or read it. Hibernate simply uses it to determine which kind of object a particular row represents.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 06, 2007 10:27 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Here's a basic outline:
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="test.tableperhierarchy">

<class name="Contact" table="contact" discriminator-value="base">
   <id name="id">
      <generator class="native"/>
   </id>
   
   <discriminator column="contact_type" type="string"/>
   
    <!-- common properties here -->

   <subclass name="CustomerContact" discriminator-value="customer">
      <!-- Customer Account and Personal Info here -->
    </subclass>

   <subclass name="BusinessContact" discriminator-value="business">
      <!-- Business Account and Business Info here -->
    </subclass>

   <subclass name="ClientContact" discriminator-value="client">
      <!-- Client Account and Business Info here -->
    </subclass>
   
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 06, 2007 10:41 am 
Newbie

Joined: Mon Aug 06, 2007 9:29 am
Posts: 3
Thanks for the very fast response.

This is the general info i found on the subject. However my Problem is, that I cannot (want not) set a column contact_type in table contact. The Entity Diagram reads:

Contact 1 to n BusinessAccount
Contact 1 to n CustomerAccount
Contact 1 to n ClientAccount

meaning 1 contact can have all 3 Attributes e.g. be a Client, be a Customer, be a BusinessPartner.

Regards
Andreas


Last edited by GhostsInc on Mon Aug 06, 2007 10:44 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 06, 2007 10:43 am 
Newbie

Joined: Mon Aug 06, 2007 9:29 am
Posts: 3
Add:
I might make a general Design mistake here. Maybe Contact is not the Base Class of BusinessContact/ CustomerContact/ ClientContact... but I havent found a better way to model their Relations.

Regards
Andreas


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 06, 2007 12:09 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
If a Contact can have all the account types then its not a hierarchy, simply a group of one-to-many properties on the Contact class.


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