EclipseLink - @OneToOne mapping does not update MongoDB(NoSQL) [message #1402526] |
Thu, 17 July 2014 05:36 |
Murthy Bhat Messages: 159 Registered: July 2009 |
Senior Member |
|
|
I'm currently working with EclipseLink (2.5.0) and MongoDB (No SQL). I am facing some issues in mapping a @OneToOne relation. When I update my Parent (which refers to a child entity), though the child is persisted correctly with an Id, the same is not reflected in the Parent. COuld someone please help me out.
My Code snippets are as below:
//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;
}
}
//OneToOneTest.java - this has the main()
public class OneToOneTest {
static EntityManagerFactory factory;
protected static EntityManager em;
public OneToOneTest() {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PersistenceUnitProperties.CLASSLOADER, OneToOneTest.class.getClassLoader());
factory = new PersistenceProvider().createEntityManagerFactory("mongo", properties);
em = factory.createEntityManager();
}
public Parent createParent(Parent parent) {
em.getTransaction().begin();
em.persist(parent);
em.getTransaction().commit();
return parent;
}
public Child createChild(Child child) {
em.getTransaction().begin();
em.persist(child);
em.getTransaction().commit();
return child;
}
public Parent readParent(String id) {
Parent p = em.find(Parent.class, id);
return p;
}
public Child readChild(String id) {
Child c = em.find(Child.class, id);
return c;
}
public Parent updateEntity(final Parent entity) {
em.getTransaction().begin();
Parent merged = em.merge(entity);
em.flush();
em.getTransaction().commit();
return merged;
}
public static void main(String[] args) throws Exception {
OneToOneTest daoService = new OneToOneTest();
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PersistenceUnitProperties.CLASSLOADER, OneToOneTest.class.getClassLoader());
daoService.factory = new PersistenceProvider().createEntityManagerFactory("mongo", properties);
EntityManager em = daoService.factory.createEntityManager();
// First clear old database.
em.getTransaction().begin();
DB db = ((MongoConnection) em.unwrap(javax.resource.cci.Connection.class)).getDB();
db.dropDatabase();
em.getTransaction().commit();
Parent parent = new Parent();
parent.setParentName("pname");
// Create
System.out.println("*************** Persisting *****************");
Child child = new Child();
child.setChildName("cname");
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()); //Child 'id' is printed correctly here.
daoService.factory.close();
}
When I run the above program, The 'Parent' and 'Child' entities are created, but Parent entity does not seem to have any link established on MongoDB. The BSON structure of the both entities are as follows :
Quote:
//Child BSON
{
"_id" : "53C67031BBD8A5FDA924868C",
"CHILDNAME" : "cname"
}
//Parent BSON
{
"_id" : "53C67031BBD8A5FDA924868D",
"PARENTNAME" : "pname",
"CHILD__id" : null
}
Could anybody please let me know what is the step that I am missing here? Am I using the OneToOne annotation correctly ?
Thanks for help.
Best Regards,
Murthy
|
|
|
|
Powered by
FUDForum. Page generated in 0.02876 seconds