三,关系映射级别注解
1. 一对一单向外键关联(两张表都要在hibernate住配置文件中进行配置)
//主表 package Entity; import javax.persistence.*; import java.sql.Timestamp; /** * Created by futao on 2017/9/27. */ @Entity @Table(name = "imooc_student") public class Student { /** * 主键,学号 */ @Id @GeneratedValue private int sid; /** * 性别 */ private String gender; /** * 生日 */ private Timestamp birthday; /** * 专业 */ private String major; /** * 身份证对象 */ @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name = "pid",unique = true) private IdCard idCard; public IdCard getIdCard() { return idCard; } public void setIdCard(IdCard idCard) { this.idCard = idCard; } 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 Timestamp getBirthday() { return birthday; } public void setBirthday(Timestamp birthday) { this.birthday = birthday; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } }
//关联表 package Entity; import org.hibernate.annotations.GenericGenerator; import javax.persistence.*; /** * Created by futao on 2017/9/27. */ @Entity @Table(name = "imoooc_id_card") public class IdCard { @Id @GeneratedValue(generator = "pid") @GenericGenerator(name="pid",strategy = "assigned") @Column(length = 18) private String pid; private String sname; private String address; public String getPid() { return pid; } public void setPid(String pid) { this.pid = pid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
//Test @Test fun dbtest() { session.save(IdCard().apply { pid = "11" sname = "熊anger" address = "上海小二房" }) session.save(Student().apply { gender = "男" birthday = Timestamp(DateTime.now().millis) major = "计算机" idCard = session.get(Student::class.java, 2) as IdCard }) }
2. 不推荐使用双向外键关联
3. 多对一单向外键关联
//多的那一方 package ManyToOne; import javax.persistence.*; import java.sql.Timestamp; /** * Created by futao on 2017/9/28. */ /** * 学生实体类 */ @Entity @Table(name = "imooc_senior_student") public class Student { @Id @GeneratedValue private int sid; private String gender; private Timestamp birthday; private String major; /** * 一对多单向外键关联关系 * 级联关系:全级连 * 抓取策略 */ //@joinColumn(name="对用生成的表中的字段名",referencedColumnName="一的那一方的主键名") @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @JoinColumn(name = "fuckyou",referencedColumnName ="cid" ) private Classroom classroom; public Classroom getClassroom() { return classroom; } public void setClassroom(Classroom classroom) { this.classroom = classroom; } 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 Timestamp getBirthday() { return birthday; } public void setBirthday(Timestamp birthday) { this.birthday = birthday; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } }
//一的那一方 package ManyToOne; import org.hibernate.annotations.GenericGenerator; import javax.persistence.*; /** * Created by futao on 2017/9/28. */ /** * 班级实体类 */ @Entity @Table(name = "imooc_senior_classroom") public class Classroom { @Id @GeneratedValue(generator = "cid") @GenericGenerator(name = "cid",strategy = "assigned") @Column(length = 4) private String cid; private String cname; public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } }
4. 多对多单向外键
多对多会生成一张中间表存储对应关系
package Many2Many; import javax.persistence.*; import java.sql.Timestamp; import java.util.Set; /** * Created by futao on 2017/9/28. */ @Entity @Table(name = "many2many_student") public class Student { @Id @GeneratedValue private int sid; private String name; private String gender; private Timestamp birthday; private String major; /** * 学生持有教师的集合 */ @ManyToMany @JoinTable( //中间表的名字 name = "student_teacher", joinColumns = {@JoinColumn(name = "sid")}, inverseJoinColumns = {@JoinColumn(name = "tid")} ) private Set<Teacher> teacherSet; public Set<Teacher> getTeacherSet() { return teacherSet; } public void setTeacherSet(Set<Teacher> teacherSet) { this.teacherSet = teacherSet; } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Timestamp getBirthday() { return birthday; } public void setBirthday(Timestamp birthday) { this.birthday = birthday; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } }
package Many2Many; import org.hibernate.annotations.GenericGenerator; import javax.persistence.*; /** * Created by futao on 2017/9/28. */ @Entity @Table(name = "many2many_teacher") public class Teacher { @Id @GeneratedValue(generator = "tid") @GenericGenerator(name = "tid", strategy = "assigned") @Column(length = 4) private String tid; private String 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; } }
@Test fun many2many(){ val t1=Teacher().apply { tid="T001" tname="张老师" } val t2=Teacher().apply { tid="t999" tname="吴彦祖" } val teachers=HashSet<Teacher>() teachers.add(t1) teachers.add(t2) val s1=Many2Many.Student().apply { name="熊老一" gender="男" birthday= Timestamp(DateTime.now().millis) major="计算机" teacherSet=teachers } val s2= Many2Many.Student().apply { name="熊老二" gender="男" birthday= Timestamp(DateTime.now().millis) major="软件工程" } val s3=Many2Many.Student().apply { name="李子" gender="女" birthday= Timestamp(DateTime.now().millis) major="建筑学" teacherSet=teachers } session.save(t1) session.save(t2) session.save(s1) session.save(s2) session.save(s3) }
5. 多对多双向外键关联
总结