Hi,
I am not happy with the schema generated for my object model. Everything works (no exceptions), but I would prefer a different kind of mapping.
I am trying to create a Person that can have multiple Addresses. The addresses should have a determined order. My List mapping looks like this:
Code:
<list name="addresses" table="uzn_personaddress" cascade="all" access="field">
<key column="id"/>
<index column="idx"/>
<composite-element class="nl.uitzendnet.impl.AddressImpl">
<property name="street" column="street" type="string"
length="32" update="true" insert="true" />
<property name="houseNr" column="houseNr" type="string"
length="8" update="true" insert="true" />
<property name="postalCode" column="postalCode" type="string"
length="8" update="true" insert="true" />
<property name="city" column="city" type="string"
length="32" update="true" insert="true" />
<property name="country" column="country" type="string"
length="8" update="true" insert="true" />
</composite-element>
</list>
The generated schema looks like this:
Code:
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id | bigint(20) | | PRI | 0 | |
| idx | int(11) | | PRI | 0 | |
| street | varchar(32) | YES | | NULL | |
| houseNr | varchar(8) | YES | | NULL | |
| postalCode | varchar(8) | YES | | NULL | |
| city | varchar(32) | YES | | NULL | |
| country | varchar(8) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
This will work perfect, but is inconvenient for me, because I need to have a single id column (because of environment restrictions). I would actually like the schema to look like this:
Code:
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id | bigint(20) | | PRI | 0 | |
| personId | bigint(20) | | FK | 0 | |
| idx | int(11) | | | | |
| street | varchar(32) | YES | | NULL | |
| houseNr | varchar(8) | YES | | NULL | |
| postalCode | varchar(8) | YES | | NULL | |
| city | varchar(32) | YES | | NULL | |
| country | varchar(8) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
Or something like that. How should I change my mapping document to get Hibernate to generate this schema?
I spend quitte some time reading the docs (especially the chapter on collections mapping) and I was very impressed by all the different mapping options. I decided on a List for my mapping because it seems most convenient for the addresses to be managed by the person object and because it offers a determined order for the addresses. If I am offtrack with using a list, feel free to set me straight :p