列表反规范化吗啡

List denormalization with morphia

假设我有两个吗啡实体:PersonTeam,它们看起来像这样

@Entity
public class Person {
   private String name;
   private String login;
   private String mail;
   private List<Team> teams;
}


@Entity
public class Team {
   private String name;
   private String description;
   private List<Person> members;
   //some more fields
}

我想像这样将这个模型映射到 Mongodb 数据库中

用户collection

{
    name:"someName",
    login:"somelogin",
    mail:"some@mail.com",
    teams: [
        {id:"teamId", name:"TeamName"} //only specific fields fron Team Entity
        {id:"anotherTeamId", name:"AnotherTeamName"}        
    ]
}

团队 collection

{
    id:"teamId", 
    name:"TeamName",
    description:"Very strong team",
    members: [id:"aaa", name: "someName"] //only specific fields fron User Entity
    //some other fields
}

{
    id:"anotherTeamId", 
    name:"AnotherTeamName",
    description:"Brave new team",
    members: [id:"aaa", name: "someName"] //only specific fields fron User Entity
    //some other fields
}

所以,我想将仅特定字段(例如仅name)从Team文档去规范化到Userteams 字段。 我不明白我可以在这种情况下使用吗啡(或其他一些 odm)吗?我应该在我的实体中使用哪些注释? List<> 字段似乎不允许 @Reference 注释。

我想,我应该创建内部 class PersonTeam,其中将包含团队的名称和 ID,并在 Person class

中使用它
@Entity
public class Person {
   private String name;
   private String login;
   private String mail;
   private List<PersonTeam> teams;
}

public class PersonTeam {
    private String teamId;
    private String teamName;
}

这是解决我问题的好方法吗?谢谢!

至少这是对吗啡唯一的方法。您可以考虑在这些字段上使用 @Reference 并仅存储 ID。如果您关心的是节省 space.

,那将会有所帮助