Many To Many的映射关系:比如有好几项任务,每个人可以接多个任务,一个任务可以由多人做。
任务Event 和 委派的人 Delegate是 Many To Many 的关系,要将这样的实体联系起来,我们需要一个额外的表,这个表中的EventId来自Event表,DelegateId来自Delegate表。
通过这个表,我们可以知道某个Event有几个Delegate,某个Delegate在做几个Event。
所以,我们的目的就是要让Hibernate帮我们生产这样的表。
我们先来看Event类,有Delegate的list
@Entity
public class Event {
private int eventId;
private String eventName;
private List<Delegate> delegates = new ArrayList<>();
}
我们在Event类中的getDelegates函数前加上ManyToMany标注
@ManyToMany
@JoinTable(name="JOIN_DELEGATE_EVENT",
joinColumns={@JoinColumn(name="eventId")},
inverseJoinColumns={@JoinColumn(name="delegateId")})
public List<Delegate> getDelegates() {
return delegates;
}
JoinTable中的
joinColumns表明,新生成的表中一个Column来自Event表中的EventId Column,
inverseJoinColumns 表明,另一个Column来自Delegate表中的DelegatedID Column。
同样的Delegate也有Event的list。
@Entity
public class Delegate {
private int delegatedId;
private String delegateName;
private List<Event> events = new ArrayList<Event>();
}
它的JointTable也是类似的,现在joinCoumns是它自己的delegateId了。
@ManyToMany
@JoinTable(name="JOIN_DELEGATE_EVENT",
joinColumns={@JoinColumn(name="delegateId")},
inverseJoinColumns={@JoinColumn(name="eventId")})
public List<Event> getEvents() {
return events;
}
这样,就设置好Many To Many Mapping了。
完整代码:
Event.java
package com.hibernate.manytomanymapping;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
@Entity
public class Event {
private int eventId;
private String eventName;
private List<Delegate> delegates = new ArrayList<>();
@Id
@GeneratedValue
public int getEventId() {
return eventId;
}
public void setEventId(int eventId) {
this.eventId = eventId;
}
public String getEventName() {
return eventName;
}
public void setEventName(String eventName) {
this.eventName = eventName;
}
@ManyToMany
@JoinTable(name="JOIN_DELEGATE_EVENT",
joinColumns={@JoinColumn(name="eventId")},
inverseJoinColumns={@JoinColumn(name="delegateId")})
public List<Delegate> getDelegates() {
return delegates;
}
public void setDelegates(List<Delegate> delegates) {
this.delegates = delegates;
}
}
Delegate.java
package com.hibernate.manytomanymapping;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
@Entity
public class Delegate {
private int delegatedId;
private String delegateName;
private List<Event> events = new ArrayList<Event>();
@Id
@GeneratedValue
public int getDelegatedId() {
return delegatedId;
}
public void setDelegatedId(int delegatedId) {
this.delegatedId = delegatedId;
}
public String getDelegateName() {
return delegateName;
}
public void setDelegateName(String delegateName) {
this.delegateName = delegateName;
}
@ManyToMany
@JoinTable(name="JOIN_DELEGATE_EVENT",
joinColumns={@JoinColumn(name="delegateId")},
inverseJoinColumns={@JoinColumn(name="eventId")})
public List<Event> getEvents() {
return events;
}
public void setEvents(List<Event> events) {
this.events = events;
}
}
TestEvent.java
package com.hibernate.manytomanymapping;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class TestEvent {
public static void main(String[] args) {
AnnotationConfiguration config = new AnnotationConfiguration();
config.addAnnotatedClass(Delegate.class);
config.addAnnotatedClass(Event.class);
config.configure();
new SchemaExport(config).create(true, true);
SessionFactory factory = config.buildSessionFactory();
Session session = factory.getCurrentSession();
session.beginTransaction();
Delegate delegate1 = new Delegate();
delegate1.setDelegateName("Alex Rod");
Delegate delegate2 = new Delegate();
delegate2.setDelegateName("Linda Berry");
Delegate delegate3 = new Delegate();
delegate3.setDelegateName("John Doe");
Delegate delegate4 = new Delegate();
delegate4.setDelegateName("James Dean");
Event event1 = new Event();
event1.setEventName("Java101");
Event event2 = new Event();
event2.setEventName("C++101");
Event event3 = new Event();
event3.setEventName("Math101");
event1.getDelegates().add(delegate1);
event1.getDelegates().add(delegate2);
event1.getDelegates().add(delegate3);
event2.getDelegates().add(delegate2);
event2.getDelegates().add(delegate3);
event3.getDelegates().add(delegate4);
session.save(delegate1);
session.save(delegate2);
session.save(delegate3);
session.save(delegate4);
session.save(event1);
session.save(event2);
session.save(event3);
session.getTransaction().commit();//will execute sql
}
}
最后,再次附上hibernate视频教程的地址:
百度云的链接
密码: xugu。
原视频在这:Java Hibernate Tutorial Part 1 - setup 。感谢Patrick提供的深入浅出的教程。