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