Kundera&MongoDB (in Play 2.1.3) bi-directional relationship doesn't get referenced correctly on the DB

StackOverflow https://stackoverflow.com/questions/18322206

Question

I'm using Kundera with Mongo and Play 2.1.3. My application has a User entity and a Document entity. The User has multiple Documents (oneToMany) and a Document belongs to a User. I first create and persist the User and the Documents can be added to that User. When I create a Document and persist it the userID that references the User is different from the userID of the User entity. And the User documents ArrayList is also always null.

So here's relevant parts of my entities:

@Entity(name="User")
@Table(name="User", schema="xpto@mongo")
public class User {

    @GeneratedValue
    @Id
    @Column(name="_id")
    private String userID;

    @Required
    @Column(name="name")
    private String name;
    (...)

    @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy="owner")
    private List<Document> documents;
    (...)
}


@Entity(name="Document")
@Table(name="Document", schema="xpto@mongo")
public class Document {

    @GeneratedValue 
    @Id
    @Column(name="_id")
    private String documentID;

    @Required
    @Column(name="name")
    private String name;

    (...)

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="userID")
    private User owner;
    (...)
}

--

The user is created and persisted. The documents are created afterwards with something like the following:

public static void createDocument(String name, User user) {
EntityManagerFactory emf = Application.getEmf();
    Document document = new Document();
    document.setName(name);
    ...
    EntityManager em = emf.createEntityManager();
    document.setOwner(user);
    em.persist(document);
    em.close();
}

In the db:
db.User.find()
{ "_id" : "5211067576aae40da0595eb0", "name" : "Name of the user" (...) }

db.Document.find()
{ "_id" : "52113c1376aae337b4537028", "name": "name", "userID" : "52113c1376aae337b4537029" }

I was expecting the userID in the document to be the same as the User _id.
As I said when I try to access the user's documents ArrayList it's always null.

What am I doing wrong here? :)
Thanks in advance.
Was it helpful?

Solution

Answered on kundera-discuss group.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top