参考:https://www.cnblogs.com/alsf/category/1113061.html
一、单向多对一
多个用户属于同一个组,多个学生属于同一个班级,多个学生属于同一个宿舍,等等,很多关系是这种多对一。因为上面是拿班级和学生来举测例子,那么所有的例子我度会用这个。为了方便和你们阅读。
多个学生属于同一个班级。单向多对一
User和Classes类的关系图。
数据库中存在外键值关系
Hibernate映射
User类
package com.test.pojo;import com.demo.pojo.BaseEntity;import org.hibernate.annotations.GenericGenerator;;import javax.persistence.*;@Entity@Table(name = "ts_user01")public class TUser { private String uid; private String username; private String password; private TClass tClass; @Id @GeneratedValue(generator = "uuid") @GenericGenerator(name = "uuid", strategy = "uuid") public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @ManyToOne() @JoinColumn(name = "cid") public TClass gettClass() { return tClass; } public void settClass(TClass tClass) { this.tClass = tClass; }}
Class类
package com.test.pojo;import com.demo.pojo.BaseEntity;import org.hibernate.annotations.GenericGenerator;import javax.persistence.*;@Entity@Table(name = "ts_class02")public class TClass { private String cid; private String name; @Id @GeneratedValue(generator = "uuid") @GenericGenerator(name = "uuid", strategy = "uuid") public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
使用Hibernate插入时 先保存class 再保存user
成功效果:
user表
class表
模型关系图
二、单向一对多
说了单向多对一,现在来说说单向一对多,根据前面我们讲的,其实就是换了一个角度去想这个问题,学生和班级,现在站在班级这方去看,肯定是需要通过班级能知晓所有在这个班学习的同学的信息。
数据库中还是没变,不管是单向一对多还是单向多对一,永远都市通过外键来维护关系的,单向多对一:通过映射文件知道,User的外键能够查找到对应的班级。单向一对多:又是怎么来实现通过User的外键找到属于本班的所有学生的呢?这就要看xx.hbm.xml映射文件如何写了。看下去你就会知道。
User 不需要多方来维护关系(维护关系的意思就是不需要通过多方找到一方的信息。)所以就不在需要many-to-one了
User类
@Entity@Table(name = "ts_user001")public class User2 { @Id @GeneratedValue(generator = "uuid") @GenericGenerator(name = "uuid", strategy = "uuid") private String uid; private String username; private String password; public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }
Class类
@Entity@Table(name = "ts_class002")public class Class2 { @Id @GeneratedValue(generator = "uuid") @GenericGenerator(name = "uuid", strategy = "uuid") private String cid; private String cname; @OneToMany(cascade= {CascadeType.ALL},fetch=FetchType.LAZY) @JoinColumn(name="cid") private Setuser2Set; 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; } public Set getUser2Set() { return user2Set; } public void setUser2Set(Set user2Set) { this.user2Set = user2Set; }}
保存方法
user表效果
class表效果
逆向数据库模型
三、双向多对一(一对多)
不管哪种叫法都市一样的,别搞混淆了。
既然知道了单向一对多,和单向多对一,这个也就很简单了,就是通过双方度能够找到对方的信息。
数据库关系:还是跟上面两种是一样的。
User类
package com.test.pojo;import org.hibernate.annotations.GenericGenerator;import org.hibernate.annotations.Table;import javax.persistence.*;@Entity@javax.persistence.Table(name = "tst_user003")public class User3 { @Id @GeneratedValue(generator = "uuid") @GenericGenerator(name = "uuid", strategy = "uuid") private String uid; private String username; private String password; @ManyToOne(cascade={CascadeType.ALL},fetch=FetchType.EAGER) @JoinColumn(name = "cid") private Class3 class3; public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Class3 getClass3() { return class3; } public void setClass3(Class3 class3) { this.class3 = class3; }}
Class类
package com.test.pojo;import org.hibernate.annotations.GenericGenerator;import javax.persistence.*;import java.util.Set;@Entity@Table(name = "tes_class003")public class Class3 { @Id @GeneratedValue(generator = "uuid") @GenericGenerator(name = "uuid", strategy = "uuid") private String cld; private String cname; @OneToMany(cascade= {CascadeType.ALL},fetch=FetchType.LAZY) @JoinColumn(name = "cid") private Setuser3Set; public String getCld() { return cld; } public void setCld(String cld) { this.cld = cld; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public Set getUser3Set() { return user3Set; } public void setUser3Set(Set user3Set) { this.user3Set = user3Set; }}
保存
成功user表效果
成功class表
逆向模型
四、建议用双向多对一 替代一对一
五、多对多
play类
package com.hibeTest.pojo;import javax.persistence.*;import java.util.HashSet;import java.util.Set;@Entity@Table(name = "player")public class Player { @Id @GeneratedValue private Integer pid; private String pname; @ManyToMany(cascade = CascadeType.ALL) @JoinTable(name = "pg", joinColumns = @JoinColumn(name = "pid"), inverseJoinColumns = @JoinColumn(name = "gid")) private Setgames = new HashSet (); public Integer getPid() { return pid; } public void setPid(Integer pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public Set getGames() { return games; } public void setGames(Set games) { this.games = games; }}
game类
package com.hibeTest.pojo;import javax.persistence.*;import java.util.HashSet;import java.util.Set;@Entity@Table(name = "game")public class Game { @Id @GeneratedValue private Integer gid; private String gname; @ManyToMany(mappedBy = "games",cascade = CascadeType.ALL) private Setplayers=new HashSet (); public Integer getGid() { return gid; } public void setGid(Integer gid) { this.gid = gid; } public String getGname() { return gname; } public void setGname(String gname) { this.gname = gname; } public Set getPlayers() { return players; } public void setPlayers(Set players) { this.players = players; }}