1

我正在将 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<>();
    ...
}

当我通过使用“cardsPinned”列表中的卡片更新卡片来存储卡片时,它存储在数据库中:

“家长”卡

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

和“儿童”卡

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

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

提前感谢您的回答。

4

1 回答 1

0

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

@OneToOne(mappedBy = "cardRef")
@DBRef
private Card cardParent;
于 2015-01-15T13:39:15.383 回答