博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate(三)对应关系
阅读量:6680 次
发布时间:2019-06-25

本文共 7520 字,大约阅读时间需要 25 分钟。

hot3.png

参考: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

e867d0076051bee7516820188adc930a3d8.jpg

成功效果:

user表

4bf159ceedc3a6020e88d340882e2607cd4.jpg

class表

38ee335073b1f8b39b64f4a34b72fcd5371.jpg

模型关系图

31e193026ff674f86eed30749de77a9d86a.jpg

二、单向一对多

        说了单向多对一,现在来说说单向一对多,根据前面我们讲的,其实就是换了一个角度去想这个问题,学生和班级,现在站在班级这方去看,肯定是需要通过班级能知晓所有在这个班学习的同学的信息。    

            

        数据库中还是没变,不管是单向一对多还是单向多对一,永远都市通过外键来维护关系的,单向多对一:通过映射文件知道,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 Set
user2Set; 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; }}

保存方法

a76ed2d1bd1fd7e26b495579f40375afede.jpg

user表效果

83d5dec726350b7a0fd6c65cb87a6079a5c.jpg

class表效果

e2e6ff28ac37f886bd814bd0165e97ed734.jpg

逆向数据库模型

6b35e45b064fa46e4804e7d1c6b060e80ab.jpg

三、双向多对一(一对多)

 不管哪种叫法都市一样的,别搞混淆了。

      既然知道了单向一对多,和单向多对一,这个也就很简单了,就是通过双方度能够找到对方的信息。

                  

 

         数据库关系:还是跟上面两种是一样的。

        

 

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 Set
user3Set; 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; }}

保存

62a4f556af8eec5da096b843ca8f44b772a.jpg

成功user表效果

9a8382856f59cf74910829199d4415d9b2b.jpg

成功class表

a4cd80a183bd44a05410d09801c325f5ece.jpg

逆向模型

e64cb58e47bfa3c4a3b3cb3fc083963e66c.jpg

四、建议用双向多对一  替代一对一

五、多对多

161155_1kFM_3234821.png

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 Set
games = 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 Set
players=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; }}

 

转载于:https://my.oschina.net/u/3234821/blog/1830227

你可能感兴趣的文章
凡客和锤子
查看>>
设计模式(5)--单例模式
查看>>
pitch yaw roll是什么
查看>>
深浅copy
查看>>
Hibernate之一级缓存
查看>>
Python基础之定义有默认参数的函数
查看>>
iOS5中的UUID
查看>>
(转载)XML Tutorial for iOS: How To Read and Write XML Documents with GDataXML
查看>>
poj 3259 Wormholes
查看>>
py学习之道
查看>>
o(1)复杂度之双边滤波算法的原理、流程、实现及效果。
查看>>
python中requests模块使用
查看>>
git bash 常用命令 新手学习
查看>>
最短路径
查看>>
POJ题目(转)
查看>>
day28 classmethod 装饰器
查看>>
QName
查看>>
Java使用线程并发库模拟弹夹装弹以及发射子弹的过程
查看>>
程序员找不女朋友的原因
查看>>
C#编程之“串口通讯多次接收”
查看>>