在 Spring mongodb 中使用双向@Dbref 是最佳实践吗

问题描述 投票:0回答:0

我们有一些

users
,一些
plots
和一段关系
users_have_plots
。详情如下:

@Data
@Document(collection = "users")
public class User {

  @Id private String id;
  private String email;
  private String password;

  @DBRef
  private List<UserPlot> plots;
}

@Data
@Document(collection = "plots")
@NoArgsConstructor
public class Plot {
  
  @Id
  private String id;
  
  private Integer x;
  private Integer y;
  
  public Plot(Integer x, Integer y) {
    this.x = x;
    this.y = y;
  }
}

@Data
@Document(collection = "users_have_plots")
public class UserPlot {
  
  @Id
  private String id;
  
  @DBRef(lazy = true)
  private User user;
  
  @DBRef(lazy = true)
  private Plot plot;

  private Status status;

  private String mnt;
}

目前,我将

UserPlot
设置为有一个 @DbRef 到
User
User
有一个 @DbRef 到
UserPlot
User
有一个 @DbRef 到
UserPlot
的原因是我想获得用户的所有情节。这种设计是否考虑了 mongo 世界中的最佳/不良实践?

如果不好,请提出解决方案。我需要在用户注册地块时管理两个属性,并有效地获取用户的所有地块。

编辑:它不是基于意见的,因为有很多关于最佳/不良实践的问题,比如宁静命名的最佳实践等

java spring mongodb spring-boot spring-data-mongodb
© www.soinside.com 2019 - 2024. All rights reserved.