wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Collection of values  question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Collection of values  question" Watch "Collection of values  question" New topic
Author

Collection of values question

Mirela Muntean
Greenhorn

Joined: Aug 17, 2007
Posts: 13
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>
kwame Iwegbue
Ranch Hand

Joined: Sep 02, 2000
Posts: 197
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.


Who dares, wins! (SAS motto)
Mirela Muntean
Greenhorn

Joined: Aug 17, 2007
Posts: 13
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

Joined: Aug 17, 2007
Posts: 13
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
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
A primary key constraint on all columns may be implicit. Have you tried to insert two identical rows into the PERSON_EMAIL_ADDR table?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collection of values question
 
Similar Threads
Foreign Key Constraint Fails
HIBERNATE- "hbm.xml" for native class as generator
HIBERNATE- "ORTOOL"
hibernate - insert object failed because of foreing key violation
Hibernate Mapping (Many-To-One Association). Problem. This forum is my last hope.