2012-11-30 50 views
5

我想使用ObjectContentManager在节点下添加节点。如何使用ObjectContentManager在节点下添加节点?

我能够添加使用ObjectContentManager单个节点,使用

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

现在这个节点下我想补充Pojo2类的其他节点。 我写了一段代码,但它给了我例外。

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

但这是给我的例外。

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 

我怎么能做到这一点? 在此先感谢。

+0

正如我阅读[上ObjectContentManager教程](http://jackrabbit.apache.org/object-content-manager.html ),您可以使用XML或Java注释设置映射描述符,以便指定持久性POjo的方式。请将映射描述符信息添加到您的问题中。 –

回答

2

如果你看一下OCM测试类有中应如何配置一个很好的例子: 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; 

豆注解是什么用来表明你的持久化对象作为另一个节点,而不是属性。

下面是添加B对象测试代码的一个目的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();