多对多映射表现为在两个表之外产生一个中间表,其主键为两个表的外键.
如下:
Person与Event为多对多关联.除了产生person,event表外,还会产生person_event表.
映射文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.lwf.hibernate.many2many">
<class name="Event" table="EVENTS">
<id name="id" column="Event_ID">
<generator class="native"/>
</id>
<property name="title"/>
<property name="date" type="timestamp" column="Event_Date"/>
<set name="participants" table="Person_Event" inverse="true">
<key column="Event_ID"/>
<many-to-many column="Person_id" class="Person"></many-to-many>
</set>
</class>
<class name="Person" >
<id name="id" column="Person_ID">
<generator class="native"/>
</id>
<property name="name"/>
<property name="age"/>
<set name="events" table="Person_Event" >
<key column="Person_id"/>
<many-to-many column="Event_ID" class="Event"/>
</set>
<set name="emailAddress" table="Person_Email_Addr" >
<key column="Person_id"/>
<element type="string" column="Emai_Addr"/>
</set>
</class>
</hibernate-mapping>
POJO
package com.lwf.hibernate.many2many;
import java.util.HashSet;
import java.util.Set;
public class Person {
private long id;
private String name;
private int age;
private Set events = new HashSet();
private Set emailAddress = new HashSet();
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Set getEvents() {
return events;
}
public void setEvents(Set events) {
this.events = events;
}
public Set getEmailAddress(){
return emailAddress;
}
public void setEmailAddress(Set emailAddress){
this.emailAddress = emailAddress;
}
}
package com.lwf.hibernate.many2many;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
public class Event {
private long id;
private String title;
private Date date;
private Set participants = new HashSet();
public Event(){}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Set getParticipants() {
return participants;
}
public void setParticipants(Set participants) {
this.participants = participants;
}
}
测试类:
package com.lwf.hibernate.many2many;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;
import org.hibernate.Session;
import com.lwf.hibernate.util.HibernateUtil;
import com.lwf.hibernate.util.MapUtil;
public class Many2ManyTest {
public static void main(String[] args) {
// addPerson();
// addEvent();
// addPersonToEvent(1L,1L);
//addEmaiAddrToPerson(1L,"zhang@126.com");
// save();
load();
}
public static void load(){
MapUtil map = new MapUtil();
map.begin();
Session session = map.getSession();
Person aPerson = (Person)session.load(Person.class, 1L);
System.out.println(aPerson.getName());
Set events = aPerson.getEvents();
for (Iterator iterator = events.iterator(); iterator.hasNext();) {
Event event = (Event) iterator.next();
System.out.println(event.getTitle());
System.out.println(event.getDate());
}
map.commit();
map.close();
}
public static void addEmaiAddrToPerson(Long personId,String emailAddr){
Session session = HibernateUtil.getSession();
session.beginTransaction();
Person aPerson = (Person)session.load(Person.class, personId);
aPerson.getEmailAddress().add(emailAddr);
HibernateUtil.commit(session);
}
public static void addPersonToEvent(Long personId, Long eventId){
Session session = HibernateUtil.getSession();
session.beginTransaction();
Person aPerson = (Person)session.load(Person.class, personId);
Event anEvent = (Event)session.load(Event.class, eventId);
aPerson.getEvents().add(anEvent);
HibernateUtil.commit(session);
}
public static void addPerson(){
Session session = HibernateUtil.getSession();
session.beginTransaction();
Person person = new Person();
person.setName("li");
person.setAge(16);
session.save(person);
HibernateUtil.commit(session);
}
public static void addEvent(){
Session session = HibernateUtil.getSession();
session.beginTransaction();
Event event = new Event();
event.setTitle("event_title2");
event.setDate(new Date());
session.save(event);
HibernateUtil.commit(session);
}
public static void save(){
MapUtil map = new MapUtil();
map.begin();
Person person = new Person();
person.setName("person1");
person.setAge(22);
Event event = new Event();
event.setTitle("title1");
event.setDate(new Date());
map.save(event);
Event event1 = new Event();
event1.setTitle("title2");
event1.setDate(new Date());
map.save(event1);
person.getEvents().add(event);
person.getEvents().add(event1);
map.save(person);
map.commit();
map.close();
}
}