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 = 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 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 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 = new HashSet<>();

}

 

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

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

你可能感兴趣的:(spring boot jpa 多表关联 @OneToOne @OneToMany @ManyToOne@ManyToMany)