现在我甚至不知道如何开始实现它。所以我有一个用户模型,它看起来像这样:
@Document
public class User {
@Id
private String id;
@Indexed(unique = true)
@NonNull
private String username;
@Indexed(unique = true)
@NonNull
private String email;
@JsonIgnore
@NonNull
private String password;
我还有一个这样的模型:
@Document("adventureholidays")
public class AdventureHolidays {
@Id
private String id;
private String title;
private String description;
private String state;
private String typeOfAdventureHolidays;
private String image;
但是问题出在哪里呢?我只提供了一个adventureholidays
,而我还有七个。
问题究竟出在哪里?我想创建一个控制器,允许用户保存最喜欢的假期,然后用户可以在个人资料中看到保存的假期。
但是,我不知道如何正确映射,因为如果我像这样编辑我的用户实体,为AdventureHolidays
添加@DBRef
@Document
public class User {
@Id
private String id;
@Indexed(unique = true)
@NonNull
private String username;
@Indexed(unique = true)
@NonNull
private String email;
@JsonIgnore
@NonNull
private String password;
@DBRef
private List<AdventureHolidays> savedTripsAdventure;
我还需要为每个其他实体添加?像
@DBRef
private List<Backpacking> savedBackpacking;
@DBRef
private List<CruiseHolidays> savedCruiseHolidays;
等等,或者有更好的方法吗?
您可以尝试在AdventureHolidays
和其他假日集合中存储对userId
的引用,而不是将DBRef
添加到用户实体中。之后,您可以使用aggregate查询获取用户的所有假期。据我所知,Mongo并不要求使用@DBRef
。