spring boot jpa 多表关联 @OneToOne @OneToMany @ManyToOne@ManyToMany

1.一对一关联 @OneToOne

import lombok.Data;

import javax.persistence.*;

/**
 * @Author: GWL
 * @Description: 一对一
 * @Date: Create in 23:10 2019/5/10
 */
@Entity
@Table(name = "t_girlfriend")
@Data
public class Girlfriend {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "g_id")
    private int id;

    @Column(name = "g_name")
    private String name;

    @Column(name = "g_age")
    private int age;

    @Column(name = "g_sweet")
    private String sweet;

    @Temporal(value=TemporalType.TIMESTAMP)
    @Column(name = "create_date")
    private java.util.Date createDate;

    @OneToOne
    @JoinColumn(name = "t_boyfriend")
    private Users user;

}

 

2.多对一和一对多  @ManyToOne

import lombok.Data;

import javax.persistence.*;

/**
 * @Author: GWL
 * @Description: 用户表
 * @Date: Create in 22:40 2019/5/3
 *
 */
@Entity
@Table(name="user")
@Data
public class Users {
    /**
     *
     *  一个角色多个用户
     *  @JoinColumn 维护外键
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id")
    private int id;

    @Column(name = "user_name")
    private String name;

    @Column(name = "user_age")
    private int age;

    @Temporal(value=TemporalType.TIMESTAMP)
    @Column(name = "create_date")
    private java.util.Date createDate;

    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "r_id")
    private Roles roles;

}

@OneToMany

import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @Author: GWL
 * @Description: 角色表 与用户表一对多,角色表与菜单表多对多
 * @Date: Create in 19:06 2019/5/8
 */
@Entity
@Table(name="t_roles")
@Getter
@Setter
public class Roles {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "role_id")
    private int roleId;

    @Column(name = "role_name")
    private  String roleName;

    @Column(name = "note")
    private String note;

    @OneToMany(mappedBy = "roles")
    private Set<Users> users = new HashSet<>();

    @ManyToMany(cascade = CascadeType.PERSIST,fetch = FetchType.EAGER)
    @JoinTable(name = "t_roles_menu",joinColumns = @JoinColumn(name = "role_id"),inverseJoinColumns = @JoinColumn(name = "menu_id"))
    private  Set<Menu> menus = new HashSet<>();

    @Override
    public String toString() {
        return "Roles{" +
                "roleid=" + roleId +
                ", roleName='" + roleName + '\'' +
                ", note='" + note + '\'' +
                '}';
    }
}

 

3.多对多 @ManyToMany

import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @Author: GWL
 * @Description: 角色表 与用户表一对多,角色表与菜单表多对多
 * @Date: Create in 19:06 2019/5/8
 */
@Entity
@Table(name="t_roles")
@Getter
@Setter
public class Roles {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "role_id")
    private int roleId;

    @Column(name = "role_name")
    private  String roleName;

    @Column(name = "note")
    private String note;
    @ManyToMany(cascade = CascadeType.PERSIST,fetch = FetchType.EAGER)
    @JoinTable(name = "t_roles_menu",joinColumns = @JoinColumn(name = "role_id"),inverseJoinColumns = @JoinColumn(name = "menu_id"))
    private  Set<Menu> menus = new HashSet<>();

    @Override
    public String toString() {
        return "Roles{" +
                "roleid=" + roleId +
                ", roleName='" + roleName + '\'' +
                ", note='" + note + '\'' +
                '}';
    }
}

 

import lombok.Data;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @Author: GWL
 * @Description: 多对多 菜单表与角色表
 * @Date: Create in 18:47 2019/5/9
 */
@Entity
@Table(name = "t_menu")
@Data
public class Menu {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "menu_id")
    private int menuId;

    @Column(name = "menu_name")
    private String menuName;

    @Column(name = "menu_url")
    private  String menuUrl;

    @Column(name = "menu_fid")
    private int fatherId;

    @ManyToMany(mappedBy = "menus")
    Set<Roles> roles = new HashSet<>();

}

 

有兴趣的朋友可以查看本人spring-boot-jpa GitHub项目

转载于:https://www.cnblogs.com/brokencolor/p/10847347.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值