@ManyToMany
@JoinTable(name = "contact_hobby_detail",
joinColumns = @JoinColumn(name = "CONTACT_ID"),
inverseJoinColumns = @JoinColumn(name = "HOBBY_ID"))
public Set<Hobby> getHobbies() {
return this.hobbies;
}
public void setHobbies(Set<Hobby> hobbies) {
this.hobbies = hobbies;
}
}
As shown in Listing 9-10, the getter method of the attribute hobbies in the Contact class are
annotated with @ManyToMany. We also provide the @JoinTable to indicate the underlying join table that
Hibernate should look for. The name is the join table's name, the joinColumns defines the column that is
the FK to CONTACT table, and the inverseJoinColumns defines the column that is the FK to the other side of
the association, i.e., the HOBBY table. Listing 9-11 shows the corresponding code snippet in the Hobby class.
Listing 9-11. Many-to-Many Mapping in the Hobby Class
package com.apress.prospring3.ch9.domain;
// Import statements omitted
@Entity
@Table(name = "hobby")
public class Hobby implements Serializable {
// Other code omitted
private Set<Contact> contacts = new HashSet<Contact>();
@ManyToMany
@JoinTable(name = "contact_hobby_detail",
joinColumns = @JoinColumn(name = "HOBBY_ID"),
inverseJoinColumns = @JoinColumn(name = "CONTACT_ID"))
public Set<Contact> getContacts() {
return this.contacts;
}
public void setContacts(Set<Contact> contacts) {
this.contacts = contacts;
}
}
The mapping is more or less the same as Listing 9-10, but the joinColumns and inverseJoinColumns
attributes are reversed to reflect the association.
The Hibernate Session Interface
In Hibernate, when interacting with the database, the major interface you need to deal with is the
Session interface, which is obtained from the SessionFactory.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home