Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAXB Eclipselink:映射摘要”;“吸气剂”;到XML_Jaxb_Eclipselink_Abstract_Moxy_Oxm - Fatal编程技术网

JAXB Eclipselink:映射摘要”;“吸气剂”;到XML

JAXB Eclipselink:映射摘要”;“吸气剂”;到XML,jaxb,eclipselink,abstract,moxy,oxm,Jaxb,Eclipselink,Abstract,Moxy,Oxm,我正在使用JAXB的EclipseLink实现(2.3)将POJO映射到XML,并遇到以下用例的问题: public abstract class A { public abstract Set<X> getX(); // There is no setter } public class B extends A { // Set via constructor private Set<X> x; @Override

我正在使用JAXB的EclipseLink实现(2.3)将POJO映射到XML,并遇到以下用例的问题:

public abstract class A {

    public abstract Set<X> getX();
    // There is no setter
}


public class B extends A {

    // Set via constructor
    private Set<X> x;

    @Override
    public Set<X> getX();

}
使用此映射也会发生同样的情况:

<java-type name="foo.A" xml-accessor-type="PROPERTY">
    <java-attributes>
        <xml-transient java-attribute="x"/>
    </java-attributes>
</java-type>

在这两种情况下,都会忽略属性“x”

我真的花了相当长的时间在这个问题上-我无法想象这是不可能的工作

我目前的解决办法是:

将foo.A保留为瞬态,为bar.B指定访问器类型字段(这可以毫无问题地获取属性“x”),并使用代码中的注释映射B中的额外属性。 但正如前面提到的:我想完全不用注释来解决这个问题——有人知道吗?布莱斯?:)

问候,


--qu

注:我是专家组的负责人和成员

你似乎碰到了一个bug。您可以通过以下链接跟踪我们在这个问题上的进展。我在下面提供了有关此问题的更多详细信息:


使用注释

如果要使用JAXB/MOXy注释映射此用例,可以在
A
类上设置
@xmlacessortype(xmlacesstype.NONE)
,并执行以下操作:

A

B

演示

输出


表示前面显示的注释的等效项:

<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum8727402">
    <java-types>
        <java-type name="A" xml-accessor-type="NONE"/>
        <java-type name="B">
            <xml-root-element/>
            <java-attributes>
                <xml-element java-attribute="x" xml-path="a/b/c/text()"/>
                <xml-element java-attribute="calculatedValue"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

演示

下面的代码演示了如何引用映射文件:

package forum8727402;

import java.util.*;
import javax.xml.bind.*;    
import org.eclipse.persistence.jaxb.JAXBContextFactory;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum8727402/oxm.xml");
        JAXBContext jc = JAXBContext.newInstance(new Class[] {A.class, B.class}, properties);

        B b = new B();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(b, System.out);
    }

}
UM8727402包;
导入java.util.*;
导入javax.xml.bind.*;
导入org.eclipse.persistence.jaxb.JAXBContextFactory;
公开课演示{
公共静态void main(字符串[]args)引发异常{
映射属性=新的HashMap(1);
put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY,“forum8727402/OXM.XML”);
JAXBContext jc=JAXBContext.newInstance(新类[]{A.Class,B.Class},属性);
B=新的B();
Marshaller=jc.createMarshaller();
setProperty(marshaller.JAXB_格式化的_输出,true);
元帅(b,系统输出);
}
}
输出

[EL Warning]:2012-01-04 14:45:46.366--忽略类[forum8727402.xml.B]上的属性[x],因为没有为其生成属性。
计算值

我目前正在调查您的问题,我将很快发布答案。太好了!某种程度上是相关的:当一个子类被封送时,超类中的属性在默认情况下不被“看到”是默认行为吗?我刚刚体验到我必须在子类中重写它们,这是冗长的…您似乎遇到了一个bug(),我们目前正在寻找修复方法。您能否提供更多关于您所看到的其他问题的详细信息:@Blaise_Doughan,非常感谢您的快速回复!我将采取我在上面发布的解决方法,并密切关注bugtracker。至于第二个问题,我决定提出一个单独的问题,因为它可能对其他人也有用:
Ignoring attribute [x] on class [bar.B] as no Property was generated for it.
<java-type name="foo.A" xml-accessor-type="PROPERTY">
    <java-attributes>
        <xml-transient java-attribute="x"/>
    </java-attributes>
</java-type>
package forum8727402;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.NONE)
public abstract class A {

    public abstract String getX();

}
package forum8727402;

import javax.xml.bind.annotation.*;    
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
public class B extends A {

    @XmlPath("a/b/c/text()")
    private String x;

    public B() {
        x = "Hello World";
    }

    @Override
    public String getX() {
        return x;
    }

    @XmlElement
    public String getCalculatedValue() {
        return "Calculated Value";
    }

}
package forum8727402;

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(B.class);

        B b = new B();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(b, System.out);
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<b>
   <a>
      <b>
         <c>Hello World</c>
      </b>
   </a>
   <calculatedValue>Calculated Value</calculatedValue>
</b>
<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum8727402">
    <java-types>
        <java-type name="A" xml-accessor-type="NONE"/>
        <java-type name="B">
            <xml-root-element/>
            <java-attributes>
                <xml-element java-attribute="x" xml-path="a/b/c/text()"/>
                <xml-element java-attribute="calculatedValue"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>
package forum8727402;

import java.util.*;
import javax.xml.bind.*;    
import org.eclipse.persistence.jaxb.JAXBContextFactory;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum8727402/oxm.xml");
        JAXBContext jc = JAXBContext.newInstance(new Class[] {A.class, B.class}, properties);

        B b = new B();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(b, System.out);
    }

}
[EL Warning]: 2012-01-04 14:45:46.366--Ignoring attribute [x] on class [forum8727402.xml.B] as no Property was generated for it.
<?xml version="1.0" encoding="UTF-8"?>
<b>
   <calculatedValue>Calculated Value</calculatedValue>
</b>