1

I am using Hibernate OGM with MongoDB and have an Entity like the following :

@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<>();
    ...
}

When I store cards through update a card with a card in the 'cardsPinned' list, it is stored in the database :

'Parent' card

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

and 'Child' card

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

My problem is that when I get data from the database, the List of pinned cards (cardsPinned java List) is always empty. Does someone know why ?

Thanks in advance for your answers.

Snorky35
  • 426
  • 6
  • 15

1 Answers1

0

Use @DBRef annotation, it create link to document in another collection

@OneToOne(mappedBy = "cardRef")
@DBRef
private Card cardParent;
David Chaava
  • 184
  • 1
  • 6