多对多单向外键
1,学生和教师构成多对多的关联关系
2,其中一个多方持有另一个多方的集合对象(学生持有教室的集合)
3,通过注解@JoinTable,创建中间表(作为多对多的载体,用来确定学生和教师的多对多关系)
@JoinTable(
name="teachars_students", //中间表的名字
joinColumns= {@JoinColumn(name="sid")}, //外键的字段
inverseJoinColumns= {@JoinColumn(name="tid")}) //反转控制字段的名字
最后会自动创建一个中间表teachars_students,并且这个表包含两个字段,sid和tid,并且这两个字段也是这个中间表的外键。
学生类:
1,这里学生持有教师的集合,所以学生类里面需要一个教师集合属性。
2,指定多对多注解:@ManyToMany
3,指定中间表注解:并分别指定学生类的主键 sid,教师类的主键 tid。@JoinTable
package mtm_fk;
import java.util.Date;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;/*JPA主键*/
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table; /*学生实体类*/
@Entity
@Table(name="Students",schema="sys")
public class Students { private int sid;
private String name;
private String gender;//性别
private Date birthday;
private String major;//专业 private Set<Teachers> teachers;//学生持有教师的集合 public Students()
{ } public Students( String name,String gender, Date birthday, String major) {
//super();
this.name=name;
this.gender = gender;
this.birthday = birthday;
this.major = major;
} @Id
@GeneratedValue //主键自动增长
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getGender() {
return gender;
} public void setGender(String gender) {
this.gender = gender;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} public String getMajor() {
return major;
} public void setMajor(String major) {
this.major = major;
}
@ManyToMany
@JoinTable(
name="teachars_students", //中间表的名字
joinColumns= {@JoinColumn(name="sid")}, //外键的字段
inverseJoinColumns= {@JoinColumn(name="tid")}) //反转控制字段的名字
public Set<Teachers> getTeachers() {
return teachers;
} public void setTeachers(Set<Teachers> teachers) {
this.teachers = teachers;
}
}
教师类:
package mtm_fk;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.annotations.GenericGenerator; //教室实体类
@Entity
public class Teachers { @Id
@GeneratedValue(generator="tid")
@GenericGenerator(name="tid",strategy="assigned")
@Column(length=4)
private String tid;//教师的编号
private String tname;//姓名 public Teachers()
{ } public Teachers(String tid, String tname) {
//super();
this.tid = tid;
this.tname = tname;
} public String getTid() {
return tid;
} public void setTid(String tid) {
this.tid = tid;
} public String getTname() {
return tname;
} public void setTname(String tname) {
this.tname = tname;
}
}
测试类:
package mtm;
import java.util.Date;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import org.junit.Test; import mtm_fk.Students;
import mtm_fk.Teachers; public class testStudents { @Test
public void testSchemaExport()
{
//创建服务注册对象
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
//创建Metadata对象
Metadata metadata =new MetadataSources(serviceRegistry).buildMetadata();
//创建SchemaExport对象
SchemaExport export = new SchemaExport();
export.create(EnumSet.of(TargetType.DATABASE),metadata);
} @Test
public void addStudetns()
{
Configuration config=new Configuration().configure();
//创建服务注册对象。
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
//创建会话工厂对象
SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry);
//创建会话对象
Session session=sessionFactory.openSession();
//开启事务
Transaction transaction=session.beginTransaction(); //创建教师对象
Teachers t1=new Teachers("T001","詹老师");
Teachers t2=new Teachers("T002","刘老师");
Teachers t3=new Teachers("T003","张老师");
Teachers t4=new Teachers("T004","陈老师"); //创建学生对象
Students s1=new Students("张三","男",new Date(),"计算机");
Students s2=new Students("李四","男",new Date(),"计算机");
Students s3=new Students("王五","男",new Date(),"计算机");
Students s4=new Students("赵六","男",new Date(),"计算机"); Set<Teachers> set1=new HashSet<Teachers>();
set1.add(t1);
set1.add(t2); Set<Teachers> set2=new HashSet<Teachers>();
set2.add(t3);
set2.add(t4); Set<Teachers> set3=new HashSet<Teachers>();
set3.add(t1);
set2.add(t3);
set3.add(t4); Set<Teachers> set4=new HashSet<Teachers>();
set3.add(t1);
set4.add(t2);
set4.add(t4); s1.setTeachers(set1);
s2.setTeachers(set2);
s3.setTeachers(set3);
s4.setTeachers(set4); session.save(t1);
session.save(t2);
session.save(t3);
session.save(t4); session.save(s1);
session.save(s2);
session.save(s3);
session.save(s4); transaction.commit();
}
}
测试结果:
1,学生:
2,教师:
3,中间表: