-->
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 bidirectional many-to-many qualified relationships
PostPosted: Thu Aug 18, 2005 1:55 am 
Newbie

Joined: Tue Aug 09, 2005 4:56 pm
Posts: 1
Hi,

I'm trying to map the following bidirectional many-to-many qualified
association:

Person [type] * ---- 0..1 Address

Where type is an attribute of the Address class. The mapping below
results in having the type column in both the join table and the address
table. How can I avoid the type column in the join table. I've
thought of using a formula, but it seems unnecessary to have another query to resolve the map key. Any ideas would be greatly appreciated.

Hibernate version: 3.0

Mapping documents:
<class name="Person">
<id name="id">
<generator class="native"/>
</id>
<property name="name"
not-null="true"
length="20"/>
<map name="addresses" table="PersonAddress" cascade="all">
<key column="id" />
<map-key column="type" type="string"/>
<many-to-many class="Address" column="addressId"/>
</map>
</class>

<class name="Address">
<id name="id">
<generator class="native"/>
</id>
<property name="type"
not-null="true"
length="20"/>
<property name="street"
length="100"/>
<set name="people" inverse="true" table="PersonAddress">
<key column="addressId"/>
<many-to-many class="Person" column="personId"/>
</set>
</class>

Name and version of the database you are using: db2 8.1

The hbm2ddl generated SQL (show_sql=true):
create table Address (
id bigint generated by default as identity,
type varchar(20) not null,
street varchar(100),
primary key (id)
)

create table Person (
id bigint generated by default as identity,
name varchar(20) not null,
primary key (id)
)

create table PersonAddress (
id bigint not null,
addressId bigint not null,
type varchar(255) not null,
personId bigint not null,
primary key (id, type)
)
Code:
Code:
public class Person {

  private Long id;

  private String name;

  private Map addresses = new HashMap();

  public Map getAddresses() {
    return addresses;
  }

  public void setAddresses(Map addresses) {
    this.addresses = addresses;
  }

  public void addAddress(Address address) {
    getAddresses().put(address.getType(), address);
    address.addPerson(this);
  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public Person() {
    super();
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

}

public class Address {

  private Long id;
  private String type;
  private String street;
  private Set people = new HashSet();

  public Address() {
    super();
  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public Set getPeople() {
    return people;
  }

  public void setPeople(Set people) {
    this.people = people;
  }

  public void addPerson(Person person) {
    getPeople().add(person);
  }

  public String getStreet() {
    return street;
  }

  public void setStreet(String street) {
    this.street = street;
  }

  public String getType() {
    return type;
  }

  public void setType(String type) {
    this.type = type;
  }

}


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.