SpringBoot JPA @OneToOne @OneToMany @ManyToOne @ManyToMany

Menu-many of which Role

Entity Relationship Diagram .png

public class User implements Serializable {
    @Id
    private Long id;

    private String name;

    private String password;

    private String phone;

    private Integer age;

    @Column(name = "role_id")
    private String roleId;

    @OneToOne(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
    @JoinColumn(name = "role_id", referencedColumnName = "id", insertable = false, updatable = false)
    private Role role;

    @OneToMany(mappedBy = "user",cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
    private List<LoginLog> loginLogList;
}
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler"})
public class Role implements Serializable {
    @Id
    private Long id;

    private String name;

    private Long sort;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "sys_role_menu", joinColumns = {@JoinColumn(name = "roleId", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "menuId", referencedColumnName = "id")})
    private List<Menu> menuList;
}
public class Menu {
    @Id
    private Long id;

    private String name;

    private String url;

    private String icon;
}
public class RoleMenu {
    @Id
    private Long Id;

    private Long RoleId;

    private Long MenuId;

}
public class LoginLog implements Serializable {
    @Id
    private Long id;

    private String userId;

    private String log;

    private Date createTime;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "userId",insertable = false,updatable = false)
    private User user;

}

User and LoginLog equipped with two-way association, the use is likely to cause a circular reference problem, to prevent this problem, the Internet is common practice to add
@JsonIgnore notes, in fact, you can use this without comment, but try to avoid calling the cycle getUser () , getLoginLogList () method.
When the query directly out of the entity classes json serialization, lazy loading is activated, getUser (), etc. is called, this time will result in a circular reference problem.

Published 476 original articles · won praise 3 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_37769323/article/details/104885522