我将昆德拉与 Mongo 和 Play 2.1.3 一起使用。我的应用程序有一个用户实体和一个文档实体。用户有多个文档(oneToMany),一个文档属于一个用户。我首先创建并保留用户,然后可以将文档添加到该用户。当我创建一个 Document 并将其持久化时,引用 User 的 userID 与 User 实体的 userID 不同。并且用户文档 ArrayList 也始终为空。
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.