• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collection of values question

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,

I don't know if the subject of the post is relevant, but here is my problem: (taken from the hibernate reference book)

I have a Person with id, age, firstname, lastname, and a set of email addresses. The addreses are stored in a different table (id, email). In this case, the primary key for this new table should be both id and email. So, how do I create this new table with hibernate that has a composite key, because in the way described in the reference manual is not working (none of the fields are marked primary key)

in the Person.hbm.xml file I have written:
<set name="emailAddresses" table="PERSON_EMAIL_ADDR">
<key column="PERSON_ID"/>
<element type="string" column="EMAIL_ADDR" />
</set>
 
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post sample of your Person class. I think Hibernate will use all the properties to make the composite key. Therefore they should be not null, unless you use idbag.
 
Mirela Muntean
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is the Person class:

public class Person {

private Long id;
private int age;
private String firstname;
private String lastname;

private Set events = new HashSet();
private Set emailAddresses = new HashSet();

public Person(){}

public Long getId(){
return this.id;
}

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


public int getAge(){
return this.age;
}

public void setAge(int age){
this.age = age;
}


public String getFirstname(){
return this.firstname;
}

public void setFirstname(String firstname){
this.firstname = firstname;
}


public String getLastname(){
return this.lastname;
}

public void setLastname(String lastname){
this.lastname = lastname;
}


public Set getEvents() {
return events;
}
public void setEvents(Set events) {
this.events = events;
}


public Set getEmailAddresses() {
return this.emailAddresses;
}
public void setEmailAddresses(Set emailAddresses) {
this.emailAddresses = emailAddresses;
}



public void addPersonToEvent(Event event){

this.getEvents().add(event);
event.getParticipants().add(this);

}

public void removePersonFromEvent(Event event){

this.getEvents().remove(event);
event.getParticipants().remove(this);
}

}
 
Mirela Muntean
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and my xml file is:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>
<class name="events.Person" table="PERSON">

<id name="id" column="PERSON_ID">
<generator class="native"/>
</id>
<property name="age" type="integer"/>
<property name="firstname" type="string" />
<property name="lastname" type="string"/>

<set name="events" table="PERSON_EVENT">
<key column="PERSON_ID"/>
<many-to-many column="EVENT_ID" class="events.Event"/>
</set>

<set name="emailAddresses" table="PERSON_EMAIL_ADDR">
<key column="PERSON_ID"/>
<element type="string" column="EMAIL_ADDR" />
</set>

</class>
</hibernate-mapping>

thank you
 
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A primary key constraint on all columns may be implicit. Have you tried to insert two identical rows into the PERSON_EMAIL_ADDR table?
 
reply
    Bookmark Topic Watch Topic
  • New Topic