无法将字符串转换为javax.jcr.Value

问题描述 投票:1回答:1

当我尝试使用以下代码为JCR节点设置属性时,出现以下错误String cannot be cast to javax.jcr.Value。javax.jcr.Node.setProperty(字符串名称,值value)需要“值”参数的值,但强制转换不起作用。

// using for-each loop for iteration over Map.entrySet() 
for (Entry<String, Object> entry : map.entrySet()) { 
    try {
        //fetch the value of uuid mapped to the key "jcr:uuid"
        String uuid= (String) map.get("jcr:uuid");
        //get the JCR workspace session value for website
        Session session = MgnlContext.getJCRSession(RepositoryConstants.WEBSITE);
        //get the JCR node specified by the given identifier
        Node node = session.getNodeByIdentifier(uuid);

        //verify if the value for the specific value is of type HashMap
        //this means we have a nested map which denotes another node
        if (entry.getValue() instanceof HashMap ) {
            System.out.println("New node: " + entry.getKey());
            //Creates a new node at relPath of the specified node type
            node.addNode(entry.getKey(),NodeTypes.Page.NAME);
            //initializes a new map that points to the nested map
            HashMap<String , Object> newmap = (HashMap<String, Object>) entry.getValue();
            //recursion happens here
            loadMap(newmap);

        } else {
            //Sets the single-value property for all entries to the specified value.

            node.setProperty(entry.getKey(), (Value)entry.getValue()); ---> *error here*
            System.out.println("Key = " + entry.getKey() + 
                            ", Value = " + entry.getValue()); 
        }       
    } catch (ItemNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (RepositoryException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

这里是解决方案,感谢@ Ducaz035

String value= (String)entry.getValue();
node.setProperty(entry.getKey(), value);
java jcr magnolia
1个回答
2
投票

我相信您可以直接使用javax.jcr.Node#setProperty(java.lang.String, java.lang.String)。实际上,它接受String作为值。

代码中的问题与字符串或值无关,但是您尝试插入对象,可能将对象转换为字符串,然后设置属性

© www.soinside.com 2019 - 2024. All rights reserved.