How to represent and query from a three entity relationship in spring boot?

假如想象 提交于 2021-01-28 13:32:51

问题


I am developing a mall system where a User can have one or more shops. If you create a shop, you have the role ADMIN for that shop, else: you create an account then you are assigned a shop as a MANAGER by the ADMIN of that shop. A user can have a shop that they are an admin, but as well be a manager to a different shop assigned to you by an owner of a that shop. Thus, i have come up with three entities: User, Role and Shop.

User Entity

 @Entity
@Table(name = "us_users")

public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@NotBlank
private String uuid;
@Email
private String us_email;
//other fields
//getters/setters
} 

Role Entity.

@Entity
@Table(name = "ro_roles")
public class Role {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private String rn_role_name;
//setter getter
}

Shop Entity.

@Entity
@Table(name = "sh_shops")
public class Shop {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    @NotBlank(message = "Shop name can not be left blank")
    private String sh_name;

    @NotBlank(message = "please provide shop icon")
    private String sh_icon;

    @NotBlank(message = "please fill the shop description")
    private String sh_description;
//other fields.. getters and setters
}

I need a relationship between these three entities: something like a table having userId,shopId,RoleId in which a user can

  1. Log in the system and the system is able to determine the roles under this user and for which shop.(Using Spring security GrantedAuthorities).
  2. Show the user shops under his account and the user can only operate a shop with correct role.

Kindly assist on how to model this use case. Thank you


回答1:


First, do you really need flexibility with the roles? Maybe having a role as a simple enum would be enough, at least if you don't plan on creating new roles at runtime. This would simplify the data model.

Second, this sounds like a map relationship for the user entity:

@Entity
@Table(name = "us_users")
public class User {

    @ManyToMany
    @MapKeyJoinColumn(name = "shop_id")
    @JoinTable(name = "user_shop_role", 
        joinColumns = @JoinColumn(name = "user_id"),
        inverseJoinColumns = @JoinColumn(name = "role_id"))
    private Map<Shop, Role> shopRoles = new HashMap<>();

}

Here, you create a three-way relationship using a user_shop_role table containing fields user_id, shop_id and role_id, meaning what you'd expect.

Then adding a shop role for the user is just a matter of adding a key-value relationship to this map:

user.getShopRoles().put(shop, role);

Showing the list of shops is just iterating through the map entries and showing a shop (key) with a corresponding role (value).

Since you use Shop as a key, you need to make sure that the Shop entity implements equals and hashCode methods correctly (based on its id).



来源:https://stackoverflow.com/questions/62793067/how-to-represent-and-query-from-a-three-entity-relationship-in-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!