-->
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: Implementing inheritance
PostPosted: Wed Jun 08, 2005 5:22 am 
Beginner
Beginner

Joined: Tue May 17, 2005 2:48 pm
Posts: 47
I have three classes:
- Person is a baseclass.
- Employee is a subclass of Person.
- Customer is a subclass of Person.

These classes correspond with the following database tables in a MSSQL database:
- tblPerson. The primary key is personid, an identity field.
- tblEmployee. The primary key is personid.
- tblCustomer. The primary key is personid.

When I make a new employee, the following must happen:
- A new record in tblPerson must be made. The identity primary key generates a new number for the primary key.
- A new record in tblEmployee must be made (with the corresponding primary key in tblPerson).


How do I set the mapping files to make this work?

Greets,
Jack


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 08, 2005 12:15 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
Code:
<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">

   <class name="Person" table="tblPerson">
      <id name="id" column="PersonID" type="Int32" unsaved-value="-1" access="field">
         <generator class="identity" />
      </id>

      ... common properties like ...      
      <property name="DateCreated" column="DateCreated" type="DateTime" not-null="true" update="false" access="nosetter.camelcase" />
      
      <joined-subclass name="Employee" table="tblEmployee">
         <key column="EmployeeID" />
         
         ... properties + relationships ...      
      </joined-subclass>
      
      <joined-subclass name="Customer" table="tblCustomer">
         <key column="CustomerID" />
         
         ... properties + relationships ...      
      </joined-subclass>
   
   </class>

</hibernate-mapping>


The Key columns correspond to the primary keysof each "implementation class" table. I'm assuming you have a PK->FK relationship between tblPerson.personid and tblEmployee.personid as well as tblPerson.personid and tblCustomer.personid.

The Hibernate Team has a sample app that provides examples that might help:

http://caveatemptor.hibernate.org/2.html

HTH,

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 08, 2005 2:36 pm 
Beginner
Beginner

Joined: Tue May 17, 2005 2:48 pm
Posts: 47
Thanks, it works fine. I made 3 seperate mapping files, one for the base class and one for every subclass.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 09, 2005 7:08 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
you may wish to change your object design, as it is flawed.. it could come back to bite you in the future.

An object can never change it's type during it's lifetime, and an object cannot belong to 2 different types


translate that into your example and you say that an employee will NEVER be a customer (and vice-versa) and NO employees can ever buy anything... this logic is flawed.

instead, try to map it where a person has a collection of Roles... and there would be a subclass for each role... CustomerRole, EmployeeRole, etc


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 1:01 pm 
Regular
Regular

Joined: Mon May 16, 2005 2:15 pm
Posts: 59
subdigital wrote:
instead, try to map it where a person has a collection of Roles... and there would be a subclass for each role... CustomerRole, EmployeeRole, etc


Interesting. I have a similar project in the future that has sematics like this. The person can be Employee and/or Contact and/or Benficiary etc.

So, if the Person object has a collection of Roles then the types in that collection aren't all the same type. Isn't that going to confuse things? How would you map the collection in the Person object which has different types?

I have the Hibernate In Action book and I don't recall discussion of this modeling symantic. Any more concrete examples you could point to would be appreciated.

Thanks,

BOb


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 16, 2005 9:27 pm 
I saw that I can have two maping file, but Can I have many maping files from many assembly files ?

Think about a extensible application. After release, the customer or third-party can develope "modules" for that application, but he need "inheritance" from the "Core" assembly and maping files.

How I can do it with Hibernate ?

Thanks,
Alexnaldo Santos


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