Hibernate version:
2.1.7
Name and version of the database you are using:
MySQL 4.1
Hi,
I have a mapping that I can't seam to get right.
I have a class User that shall have a Map with addresses, The keys for the adresses shall be defined in the database in the class AddressType, and the value (the address) is an other class Address that is an ValueObject.
The idea is that the users can have 0-n Addresses but only of the types defined in AddressType. Example data for AddressType is {{1,"Home Address"}, {2, "Work Address"},...}
Address looks like
Code:
public class Address {
private String address1;
private String address2;
private String postalCode;
private String postalAddress;
private String country;
...
}
AddressType looks like
Code:
* @hibernate.class table="addressType"
public class AddressType {
private Long id;
private String name;
...
* @hibernate.id column="addressTypeId" unsaved-value="null" generator-class="increment"
public Long getId() {
...
* @hibernate.property length="100" not-null="true" unique="true"
public String getName() {
...
}
User looks like
Code:
* @hibernate.class table="user"
* @author Kristoffer Skjutare
* @created 2005-jan-10 15:49:13
*/
public class User implements AutoDateAware {
private Long id;
private String email;
private String firstName;
private String lastName;
private String password;
...
private Map address;
...
/**
* Retrievs the Id.
* @hibernate.id column="userId" unsaved-value="null" generator-class="hilo"
* @return the id.
*/
public Long getId() {
return id;
}
...
* This is the first atempt, didn't get it to work...
* hibernate.map table="userAddress" lazy="true" cascade="all"
* hibernate.collection-key column="userId"
* hibernate.collection-index column="addressTypeId" type="long"
* hibernate.collection-element class="watson.backend.dao.Address"
*/
public Map getAddress() {
return address;
}
...
}
The second atempt is made as a real mapping that xdoclet include.
Code:
<map name="userAddress" table="userAdresses" lazy="true" cascade="all">
<key column="userId" />
<composite-index class="watson.backend.dao.AddressType">
<key-many-to-one name="id" column="addressTypeId" />
</composite-index>
<composite-element class="watson.backend.dao.Address">
<property name="address1" length="100" type="string" />
<property name="address2" length="100" type="string" />
<property name="postalCode" length="10" type="string" />
<property name="postalAddress" length="100" type="string" />
<property name="country" length="100" type="string" />
</composite-element>
</map>
But this done seam to work either.
How shall I do it? Or am I totaly wrong here? Shall it be designed in another way?
Best Regards
Kristoffer Skjutare