Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How i can add a node under a node using ObjectContentManager?

I want to add a node under a node using ObjectContentManager.

I am able to add a single node using ObjectContentManager , using

Pojo1 p1 = new Pojo1 ();
p1 .setPath("/p1");
p1 .setName("p_3");
p1 .insert(p1);
ocm.save();

Now under this node I want to add another node of Pojo2 class. I have written a code , but it is giving me exception.

Pojo2 p2 = new Pojo2 ();
p2.setPath("/p1/p2");
p2.setName("p_3");
p2.insert(p2);
ocm.save();

But this is giving me exception.

org.apache.jackrabbit.ocm.exception.ObjectContentManagerException: Cannot create new node of type nt:pojo1 from mapped class class com.sapient.Pojo1; nested exception is javax.jcr.nodetype.ConstraintViolationException: No child node definition for p2 found in node /p1

How i can achieve this? Thanks in advance.

like image 572
Thinker Avatar asked Nov 04 '22 09:11

Thinker


1 Answers

If you look at the OCM test classes there's a good example of how this should be configured: A.java

@Node(jcrMixinTypes="mix:lockable" )
public class A
{
@Field(path=true) private String path;
@Field private String a1;
@Field private String a2;
@Bean(jcrType="nt:unstructured", jcrOnParentVersion="IGNORE") private B b;

The Bean Annotation is what's used to indicate that your persisting the object as another node rather than a property.

Here's the test code that adds the B object the A object AnnotationBeanDescriptorTest.java

ObjectContentManager ocm = getObjectContentManager();
// ------------------------------------------------------------------------
// Create a main object (a) with a null attribute (A.b)
// ------------------------------------------------------------------------
A a = new A();
a.setPath("/test");
a.setA1("a1");
ocm.insert(a);
ocm.save();

// ------------------------------------------------------------------------
// Retrieve
// ------------------------------------------------------------------------
a = (A) ocm.getObject("/test");
assertNotNull("Object is null", a);
assertNull("attribute is not null", a.getB());

B b = new B();
b.setB1("b1");
b.setB2("b2");
a.setB(b);

ocm.update(a);
ocm.save();
like image 58
Bob Paulin Avatar answered Nov 14 '22 04:11

Bob Paulin