-->
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: Hibernate inheritance - Person/Client/Employee
PostPosted: Fri Aug 05, 2005 3:05 pm 
Newbie

Joined: Fri Aug 05, 2005 2:27 pm
Posts: 1
Hi,

I implement the inheritance with table per-subclass strategy,
but i have some problems:

I have 1 mapping file and 3 POJO's that represent this 3
tables:

===============================================
PERSON
--------
ID (Primary Key)
NAME
EMAIL

CLIENT
--------
ID (Primary Key and Foreign Key to table Person)
CREDITCARD

EMPLOYEE
---------
ID (Primary Key and Foreign Key to table Person)
SALARY
===============================================

The classes Client and Employee extends Person.

I have the situation that one client can be a employee too.
If I include a new Client, a new id is generated.
If I get this Id and create a new Employee with this, when
I save, a new Id is generated and a new Person is created.
My goal was that only a new Employee was inserted, the person
was exist in the Person table.
How I can do this??

My mapping-files:

===============================================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>

<class name="com.transportes.Person"
table="person">

<id name="id"
type="java.lang.Long"
column="id">

<generator class="sequence">
<param name="sequence">person_id_seq</param>
</generator>
</id>

<property name="name"
type="java.lang.String"
column="name"
not-null="true"
length="60"/>

<property name="email"
type="java.lang.String"
column="email"
not-null="false"
length="60"/>

<joined-subclass
name="com.transportes.Client"
table="client">

<key column="id"/>

<property
name="creditCard"
type="java.lang.String"
column="creditCard"
not-null="false"
length="10"/>
</joined-subclass>

<joined-subclass
name="com.transportes.Employee"
table="employee">

<key column="id"/>

<property
name="salary"
type="java.lang.Long"
column="salary"
not-null="false"/>
</joined-subclass>

<joined-subclass
name="com.transportes.vo.ProprietarioVO"
table="proprietario">

<key column="id"/>

<property
name="nascimento"
type="java.util.Date"
column="nascimento"
not-null="false"/>
</joined-subclass>

</class>
</hibernate-mapping>

===============================================

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 06, 2005 4:02 am 
Newbie

Joined: Sat May 21, 2005 8:02 am
Posts: 6
When you create an instance of "client", you get two new rows: one in the table
"client", a second in the table "person".
Now you want to create an "employee" that uses the row in table "person" as well as
the "client" does, right?

This is not possible. In fact it's wrong. Logically the client-object consists of two rows.
An employee consist of two rows, one in table "employee", a second in table "person".

My suggestion: Do not use inheritance. Use Delegation. Have three classes
"person", "client" and "employee" and a one-to-one association between client and
person on the one hand and a one-to-one association between employee and
person.
That way a client "is a" person and an employee "is a" person. Both point to the same
person, and you have just one row in table "person".

Hope this helps.
Erik


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.