-->
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.  [ 5 posts ] 
Author Message
 Post subject: Hibernate Inheritance Mapping - is it possible?
PostPosted: Fri Mar 26, 2004 6:09 pm 
Newbie

Joined: Fri Mar 26, 2004 3:30 pm
Posts: 4
Hi,

I am having great difficulty trying to model the following classes using Hibernate. I do feel it may not be possible to do. Please help.


class Address {
... // typical address attributes
}

class CustomerAddress extends Address {
String addressRef; // e.g. home, work, friend
int timeAtAddress;
...
}

class Supplier {
Address address;
...
}

class Customer {
Set addresses;
...
}

A Supplier will have one address.
A Customer will have 1 or more CustomerAddress instances.
(Later, there may be further subclasses of Address)
--------------------------------------------------------

At the database, I would expect something like:

[Supplier] 1 ------------ 1 [Address]

[Customer] 1 ----------<- 1..n [Address(CustomerAddress)]


I have been totally baffled trying to write the Hibernate mapping file(s) for these classes.

The above may not be possible to do. Any help would be greatly appreciated.

I am using Hibernate versation 2.1.1 and may easily use a later version if it helps.

Thanks in advance,

Usman


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 6:12 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
of course it is possible, what db schema do you have?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 27, 2004 7:03 am 
Newbie

Joined: Fri Mar 26, 2004 3:30 pm
Posts: 4
Thank you for the quick reply. I apologise for not supplying enough information.

I do not have a DB schema yet. In fact, it is the DB schema that is confusing me. I'll try to explain next.

I am modelling a class hierarchy in UML from which I intend to write the Hibernate mapping files. I will use the SchemaExport tool to generate the database tables and relationships from the .hbm files. Hence, I am hoping this SchemaExport tool will create the necessary tables and relationships in the MySQL database I am using rather than I directly working at the database level.

The following is the outline java code for the UML hierarchy:

public class Address {
private int buildingNumber;
private string street;
private string town;
private string region;
}

public class CustomerAddress extends Address {
private date dateMovedIn;
private string addressType;
private int noOfYearsAtAddress;
}

public class Customer {
private string customerRef;
private string forename;
private string surname;
private set customerAddresses;

//set of CustomerAddress instances, not base class Address:
private Set customeraddresses;
}

public class Supplier {
private string supplierRef;
private string name;
private Address supplierAddress;

private Address address;
}


The following are my .hbm stubs and are incomplete - this is where I am stuck:


<class name="mypkg.Customer"
table="Customer" >
<id
name="id"
type="java.lang.Integer"
unsaved-value="0" >
<generator class="native" />
</id>

<property
name="customerRef"
type="java.lang.String" />

... here I need some kind of reference to the CustomerAddress class?
</class>

<class name="mypkg.Supplier"
table="Supplier" >
<id
name="id"
type="java.lang.Integer"
unsaved-value="0" >
<generator class="native" />
</id>

<property
name="supplierRef"
type="java.lang.String" />

<!-- is the following correct? -->
<many-to-one name="address"
column="ADDRESS"
class="Address"/>
</class>

<class name="mypkg.Address"
table="Address" >
<id
name="id"
type="java.lang.Integer"
unsaved-value="0" >
<generator class="native" />
</id>

<property
name="street"
type="java.lang.String" />
...
<subclass
name="mypkg.CustomerAddress"
discriminator-value="CUSTOMERADDRESS"
>
<property
name="dateMovedIn"
type="java.util.Date" />
<property
name="addressType"
type="java.lang.String" />
</subclass>
</class>

What do I specify in the Customer mapping to define the Customer----<-CustomerAddress relationship?
How I get the Customer to reference CustomerAddress rather than Address?


Thank you,

Usman


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 29, 2004 4:25 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
<class name="mypkg.Customer"
table="Customer" >
<set name="customeraddresses">
<key name=""/>
<one-to-many class="CustomerAddress"/>
</set>


<class name="mypkg.Supplier"
table="Supplier" >

<!-- is the following correct? --> <!-- yes -->
<many-to-one name="address"
column="ADDRESS"
class="Address"/>
</class>

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 29, 2004 5:19 am 
Newbie

Joined: Fri Mar 26, 2004 3:30 pm
Posts: 4
Thanks guys for all your help.


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