【问题标题】:How to do Relationship ManytoMany with Java Spring Boot JPA如何使用 Java Spring Boot JPA 进行关系多对多
【发布时间】:2021-03-09 10:18:50
【问题描述】:

我想更新我的应用程序,目前,我有用户和一般类别列表,我想添加用户的类别列表,但拥有所有类别的管理员用户除外。 所以我在考虑做一个多对多的关系。我看到没有必要只制作一个 ID 表。

我使用 JPA 和 MongoDB 作为我的数据库的 Spring Boot。

\\Models
@Document(collection = "users")
public class AppUser {

    @Transient
    public static final String SEQUENCE_NAME = "users_sequence";

    @Id
    private int id;

    @NotBlank
    @Size(max = 20)
    private String username;

    @NotBlank
    @Size(max = 120)
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private String password;

    @DBRef
    private  AppRole role = new AppRole();

    @ManyToMany(mappedBy = "categories")
    private  List<AppCategory> lstCategory = new ArrayList();
}
@Document(collection = "categories")
public class AppCategory {

    @Transient
    public static final String SEQUENCE_NAME = "category_sequence";
    @Id
    private int id;
    private String libelle;
}

第三个表版本

public class AppUser {
...
 @ManyToMany
    @JoinTable(
      name = "usersCategories", 
      joinColumns = @JoinColumn(name = "user"), 
      inverseJoinColumns = @JoinColumn(name = "Category"))
    private  List<AppCategory> lstCategorie = new ArrayList<AppCategory>();
...
}
@Document(collection = "usersCategories")
public class AppUserCategory {

@DBRef
private  AppUser User = new AppUser();
@DBRef
private  AppCategory Category = new AppCategory();

}

【问题讨论】:

  • 对于关系数据库,您必须使用第三个表进行 id-id 映射以支持多对多。在 Mongo 中,您可能希望在文档中存储 id 数组,但我不确定您将如何准确映射它,因为我自己还没有使用 JPA/Mongo 组合。但是,请注意,@ManyToMany(mappedBy = "tags") 实际上需要 tags 中的 tags 属性。
  • 查看这篇文章Designing Many-to-Many relationships in MongoDB (instead of relational tables)。有关有用信息,请参阅 MongoDB 文档:Data Modeling Introduction.
  • 我读到有两种可能性,一种带有第三张表,另一种只有注释。我添加了第三张表版本但不起作用。您需要 DOA 和服务吗?

标签: java spring mongodb spring-boot


【解决方案1】:
\\Models
@Document(collection = "users")
public class AppUser {

    @Transient
    public static final String SEQUENCE_NAME = "users_sequence";

    @Id
    private int id;

    @NotBlank
    @Size(max = 20)
    private String username;

    @NotBlank
    @Size(max = 120)
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private String password;

    @DBRef
    private  AppRole role = new AppRole();

    @DBRef
    private  List<AppCategory> lstCategory = new ArrayList();
}
@Document(collection = "categories")
public class AppCategory {

    @Transient
    public static final String SEQUENCE_NAME = "category_sequence";
    @Id
    private int id;
    private String libelle;
}
//exemple user
{
  "_id": 1,
  "username": "myusername",
  "email": "myusername@gmail.com",
  "password": "",
  "role": {
    "$ref": "role",
    "$id": 1
  },
  "lstCategory": [
    {
      "$ref": "categories",
      "$id": 2
    },
    {
      "$ref": "categories",
      "$id": 3
    }

只需要@DBRef

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 2021-10-06
    相关资源
    最近更新 更多