gpt4 book ai didi

java - 我如何使用 ObjectContentManager 在节点下添加节点?

转载 作者:搜寻专家 更新时间:2023-10-31 20:29:33 25 4
gpt4 key购买 nike

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

但这给了我异常(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

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

最佳答案

如果您查看 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 注释用于指示您将对象持久化为另一个节点而不是属性。

这是将 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();

关于java - 我如何使用 ObjectContentManager 在节点下添加节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13640525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com