Hibernate OGM OneToMany自我参考

时间:2014-07-02 16:24:30

标签: java mongodb hibernate-ogm

我正在使用Hibernate OGM和MongoDB,并拥有如下所示的实体:

@Entity
public class Card implements Serializable, Comparable<Card>
{
    private static Logger logger = LoggerFactory.getLogger(Card.class);

    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    private String  id;
    ...

    @OneToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    private Card    cardRef;

    @OneToOne(mappedBy = "cardRef")
    private Card cardParent;

    // pinning : represents the owner's card
    @ManyToOne
    private Card cardPinOrigin;

    // List of the cards that have pinned the current
    @OneToMany(mappedBy = "cardPinOrigin")
    private List<Card> cardsPinned = new ArrayList<>();
    ...
}

当我通过卡片更新卡片来存储卡片时,可以使用卡片进行卡片制作。列表,它存储在数据库中:

&#39;父&#39;卡

{
    "_id" : "39d83887-e466-45c5-a5e8-1dfd5c22ed8a",
    "cardPinOrigin" : [ 
        {
            "id" : "478f6f83-161f-44dc-86c4-844f73084ffb"
        }
    ],
    ....
}

和&#39;孩子&#39;卡

{
    "_id" : "478f6f83-161f-44dc-86c4-844f73084ffb",
    "cardPinOrigin_id" : "39d83887-e466-45c5-a5e8-1dfd5c22ed8a",
    ...
}

我的问题是,当我从数据库中获取数据时,固定卡列表(cardsPinned java List)始终为空。有人知道为什么吗?

提前感谢您的回答。

1 个答案:

答案 0 :(得分:0)

使用@DBRef注释,它会创建另一个集合中的文档链接

@OneToOne(mappedBy = "cardRef")
@DBRef
private Card cardParent;