我目前正在使用EclipseLink和MongoDB。我有一个案例,我有一个@OneToOne映射。以下是代码 -
//Parent.java
@Entity
@NoSql(dataFormat = DataFormatType.MAPPED)
public class Parent {
@Id
@GeneratedValue
@Field(name = "_id")
private String id;
private String parentName;
@OneToOne(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
private Child child;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getParentName() {
return parentName;
}
public void setParentName(String parentName) {
this.parentName = parentName;
}
public Child getChild() {
return child;
}
public void setChild(Child child) {
this.child = child;
}
}
//Child.java
@Entity
@NoSql(dataFormat = DataFormatType.MAPPED)
public class Child {
@Id
@GeneratedValue
@Field(name = "_id")
private String id;
private String childName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getChildName() {
return childName;
}
public void setChildName(String childName) {
this.childName = childName;
}
}
使用上面的类,我按以下顺序保存了对象。
Parent parent = new Parent();
parent.setParentName("pname");
// Create
System.out.println("*************** Persisting *****************");
Child child = new Child();
child.setChildName("cname");
daoService.createChild(child);
Parent p = daoService.createParent(parent);
p.setChild(child);
daoService.updateEntity(p);
Parent parent2 = daoService.readParent(p.getId());
System.out.println("Child : " + parent2.getChild());
Child c = daoService.readChild(parent2.getChild().getId());
System.out.println("Child id : " + c.getId());
但是,我发现在MongoDB中,Parent和Child对象之间没有链接。
持久性步骤后MongoDB的BSON结构如下。
{
"_id" : "53C67031BBD8A5FDA924868D",
"PARENTNAME" : "pname",
"CHILD__id" : null
}
{
"_id" : "53C67031BBD8A5FDA924868C",
"CHILDNAME" : "cname"
}
请有人告诉我,我在这里做错了什么。