4

別のプロジェクトに、変更できない 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 を変更せずに電話番号のマーシャリングを抑制する方法はありますか?

4

1 に答える 1

2

次のマッピング ファイルを使用して、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 マッピング ファイルの詳細については、次を参照してください。

于 2011-03-28T14:00:28.380 に答える