如何在Spring Boot中从三个实体关系表示和查询? [英] How to represent and query from a three entity relationship in spring boot?

查看:82
本文介绍了如何在Spring Boot中从三个实体关系表示和查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个购物中心系统,其中用户可以拥有一个或多个商店.如果创建商店,则该商店的角色为 ADMIN ;否则:创建一个帐户,然后 ADMIN将您分配为 MANAGER 的商店.用户可以拥有自己的管理员商店,也可以成为其他商店经理该商店的所有者分配给您的.因此,我提出了三个实体:用户,角色和商店.

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.

用户实体

 @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
} 

角色实体.

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

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

    private String rn_role_name;
//setter getter
}

商店实体.

@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
}

我需要这三个实体之间的关系:类似于具有userId,shopId,RoleId的表,用户可以在其中进行

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

  1. 登录系统,然后系统可以确定该用户下的角色以及在哪家商店中工作.(使用Spring安全性GrantedAuthorities).
  2. 向用户显示其帐户下的商店,用户只能以正确的角色来经营商店.

请协助您对该用例进行建模.谢谢

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

推荐答案

首先,您真的需要灵活的角色吗?至少作为一个简单的枚举角色就足够了,至少如果您不打算在运行时创建新角色.这样可以简化数据模型.

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<>();

}

在这里,您可以使用包含字段user_idshop_idrole_iduser_shop_role表创建三向关系,这意味着您所期望的.

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).

由于使用Shop作为键,因此需要确保Shop实体正确地实现equalshashCode方法(基于其id).

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).

这篇关于如何在Spring Boot中从三个实体关系表示和查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆