5

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.

Thinker
  • 6,820
  • 9
  • 27
  • 54
  • As I read the [tutorial on ObjectContentManager](http://jackrabbit.apache.org/object-content-manager.html), you set up a mapping descriptor with XML or Java annotations in order to specify how your pojo is to be persisted. Please add the mapping descriptor information to your question. – David Gorsline Dec 03 '12 at 18:34

1 Answers1

2

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();
Bob Paulin
  • 1,088
  • 8
  • 13