-->
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.  [ 3 posts ] 
Author Message
 Post subject: Difficulties Implementing a constrained relationship.
PostPosted: Wed Feb 23, 2005 4:59 pm 
Newbie

Joined: Sun Oct 31, 2004 3:22 pm
Posts: 6
Hibernate version:2.1
Mapping document #1: A simple address class
<?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="maps.Address"
table="address"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="id"
column="id"
type="java.lang.Long"
unsaved-value="null"
>
<generator class="hilo">
</generator>
</id>
<version
name="version"
type="java.lang.Long"
column="version"
access="property"
unsaved-value="undefined"
/>
<property
name="city"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="city"
length="50"
/>
<list
name="personList"
lazy="false"
inverse="false"
cascade="save-update"
>
<key
column="address"
>
</key>

<index
column="ndx_addrmap"
type="java.lang.Long"
/>
<one-to-many
class="maps.AddressMap"
/>
</list>
<property
name="state"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="state"
length="2"
/>
<property
name="street"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="street"
length="50"
/>
<property
name="zip"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="zip"
length="10"
/>
</class>
</hibernate-mapping>
Mapping document #1: A simple person class
<?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="maps.Person"
table="person"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="id"
column="id"
type="java.lang.Long"
unsaved-value="null"
>
<generator class="hilo">
</generator>
</id>
<version
name="version"
type="java.lang.Long"
column="version"
access="property"
unsaved-value="undefined"
/>
<list
name="addressList"
lazy="false"
inverse="false"
cascade="save-update"
>
<key
column="person"
>
</key>
<index
column="ndx_personmap"
type="java.lang.Long"
/>
<one-to-many
class="maps.AddressMap"
/>
</list>
<property
name="name"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="name"
/>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 23, 2005 5:41 pm 
Newbie

Joined: Sun Oct 31, 2004 3:22 pm
Posts: 6
The original post was incomplete. My apologies to the readers.

The problem that I am trying to solve is implementing a constrained relationship between classes. This is an architectural issue for a project that we are currently working on.

I am using a person-address mapping for illustration. A person can have many addresses, and an address can be used by many people. The relationship is constrained by some usage criteria (Home Address, Offoce Address, etc.)

I have setup a Person <-->AddressMap<-->Address association using those three classes.
I am using one-to-many cardinality from Person to AddressMap
I am using many-to-one cardinality from AddressMap to Person.
The same is true between Address and AddressMap.

The third mapping file is listed below. The implementation is working without error, but seems inefficient on the database side. Hibernate generates the following schema for the mapping table:
create table address_of (
id int8 not null,
version int8 not null,
fk_address int8,
addressOf varchar(50),
fk_person int8,
address int8,
ndx_addrmap int4,
person int8,
ndx_personmap int4,
primary key (id)
);

the fields fk_address and address are redundant. They will always contain the same values
the fields fk_person and person are also redundant, for the same reason. I have not seen any examples of how others are doing this. Most of the books I have read do not address it. That extra 16 bytes is going to use up a lot of disk space, given the number of records.
I have only been working with Hibernate for about 6 months and I am sure this is something that I am doing wrong or have misunderstood. I would appreciate any insights that anyone might have.




Mapping document #3: A very simple address map class
<?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="maps.AddressMap"
table="address_of"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="id"
column="id"
type="java.lang.Long"
unsaved-value="null"
>
<generator class="hilo">
</generator>
</id>
<version
name="version"
type="java.lang.Long"
column="version"
access="property"
unsaved-value="undefined"
/>
<many-to-one
name="address"
class="maps.Address"
cascade="save-update"
outer-join="auto"
update="true"
insert="true"
access="property"
column="fk_address"
/>
<property
name="addressOf"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="addressOf"
length="50"
/>
<many-to-one
name="person"
class="maps.Person"
cascade="save-update"
outer-join="auto"
update="true"
insert="true"
access="property"
column="fk_person"
/>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 23, 2005 7:10 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This is really just a standard parent/child association. Read that section in the user guide.


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