-->
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.  [ 1 post ] 
Author Message
 Post subject: Mapping a Map where Key is AddressType and Value is Address?
PostPosted: Wed Jan 12, 2005 4:41 am 
Newbie

Joined: Wed Aug 11, 2004 7:01 am
Posts: 10
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.