5

我想使用 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

我怎样才能做到这一点?提前致谢。

4

1 回答 1

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;

Bean Annotation 用于指示您将对象持久化为另一个节点而不是属性。

这是将 B 对象添加到 A 对象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();
于 2012-12-08T03:37:16.743 回答