【问题标题】:How does MOXy's xml-transient declaration work?MOXy 的 xml-transient 声明是如何工作的?
【发布时间】:2011-03-28 13:27:36
【问题描述】:

我在一个单独的项目中有一组无法更改的 bean。这些 bean 具有 JPA 和 JAXB 注释,并且正在 RESTful 实现中使用。我的大部分关系都是延迟加载的,我希望能够更精细地控制哪些元素实际上被编组以进行传输。

我在下面得到了修改后的 MOXy Customer.java 类。

@javax.xml.bind.annotation.XmlType
@javax.xml.bind.annotation.XmlAccessorType(value=javax.xml.bind.annotation.XmlAccessType.PROPERTY)
public class Customer {

  private String name;
  private Address address;
  private List<PhoneNumber> phoneNumbers;

  // getters and setters
}

我希望能够使用 MOXy eclipselink-oxm 映射来控制编组的内容,但它的行为与我预期的不同。使用 JAXB 注释,您将元素(字段或属性)声明为瞬态,但 eclipselink-oxm.xml 仅允许对类型进行瞬态声明。但是,当我像这样声明类型瞬变时,会出现以下异常:

<?xml version="1.0"?>
<xml-bindings 
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm">
<java-types>
    <java-type name="example.gettingstarted.Customer">
        <xml-root-element/>
        <java-attributes>
            <xml-element java-attribute="name" xml-path="personal-info/name/text()"/>
            <xml-element java-attribute="address" xml-path="contact-info/address"/>
        </java-attributes>
    </java-type>

    <java-type name="example.gettingstarted.PhoneNumber" xml-transient="true" />

</java-types>
</xml-bindings>

例外:

Exception [EclipseLink-110] (Eclipse Persistence Services - 2.1.0.v20100614-r7608):     org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Descriptor is missing for class [example.gettingstarted.PhoneNumber].
Mapping: org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping[phoneNumbers]
Descriptor: XMLDescriptor(example.gettingstarted.Customer --> [DatabaseTable(customer)])

如果我删除 xml-transient 属性或将其设置为 false,则 Customer 将按预期转换为 XML。有什么方法可以在不修改 Customer bean 的情况下抑制电话号码的编组?

【问题讨论】:

    标签: java jaxb eclipselink moxy


    【解决方案1】:

    您可以指定使用以下映射文件来使 Customer 上的“phoneNumbers”属性为瞬态:

    <?xml version="1.0"?>
    <xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm">
        <java-types>
            <java-type name="example.gettingstarted.Customer">
                <xml-root-element />
                <java-attributes>
                    <xml-element java-attribute="name" xml-path="personal-info/name/text()" />
                    <xml-element java-attribute="address" xml-path="contact-info/address" />
                    <xml-transient java-attribute="phoneNumbers"/>
                </java-attributes>
            </java-type>
        </java-types>
    </xml-bindings>
    

    有关 MOXy 的 XML 映射文件的更多信息,请参阅:

    【讨论】:

    • 完美。这行得通。我应该更仔细地阅读 .xsd。
    猜你喜欢
    • 1970-01-01
    • 2013-06-28
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多