2012-03-09 8 views
8

まず、私はMarshaller#Listenerについて話していません。 私はそれらのclass definedイベントコールバックについて話しています。JAXB `beforeMarshal(Marshaller)`メソッドから返されるものは何ですか?

boolean beforeMarshal(Marshaller)メソッドから返される内容を教えてもらえますか?

/** 
* Where is apidocs for this method? 
* What should I return for this? 
*/ 
boolean beforeMarshal(Marshaller marshaller); 

私はMOXYなしJAXB-RIとJPA's Long @Id to JAXB's String @XmlIDを変換するため、この方法を使用することが、とにかく、意味。

[編集] voidバージョンが動作しているようです。これは単なるドキュメントの問題ですか?

答えて

7

短い答え

boolean戻り値の型は、ドキュメントエラーです。戻り値の型はvoidである必要があります。

ロング回答私は意味

が、とにかく、 JAXBのStringにJPAのロング@Idを変換するため、この方法を使用するためには、それが持っていないとして、あなたはEclipseLink JAXB (MOXy)を使用することができ

を@XmlID @XmlIDで注釈を付けられたフィールド/プロパティがStringであるという制限。

JAXB-RIとMOXyなし。

あなたのユースケースをサポートマップするために XmlAdapterを使用することができ

IDAdapter

このXmlAdapter@XmlID注釈の要件を満たすためにString値にLong値に変換します。@XmlJavaTypeAdapter注釈がXmlAdapterを指定するために使用され

package forum9629948; 

import javax.xml.bind.DatatypeConverter; 
import javax.xml.bind.annotation.adapters.XmlAdapter; 

public class IDAdapter extends XmlAdapter<String, Long> { 

    @Override 
    public Long unmarshal(String string) throws Exception { 
     return DatatypeConverter.parseLong(string); 
    } 

    @Override 
    public String marshal(Long value) throws Exception { 
     return DatatypeConverter.printLong(value); 
    } 

} 

B

package forum9629948; 

import javax.xml.bind.annotation.*; 
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 

@XmlAccessorType(XmlAccessType.FIELD) 
public class B { 

    @XmlAttribute 
    @XmlID 
    @XmlJavaTypeAdapter(IDAdapter.class) 
    private Long id; 

} 

package forum9629948; 

import javax.xml.bind.annotation.*; 

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
public class A { 

    private B b; 
    private C c; 

} 

C

package forum9629948; 

import javax.xml.bind.annotation.*; 

@XmlAccessorType(XmlAccessType.FIELD)public class C { 

    @XmlAttribute 
    @XmlIDREF 
    private B b; 

} 

デモ

package forum9629948; 

import java.io.File; 
import javax.xml.bind.*; 

public class Demo { 

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

     File xml = new File("src/forum9629948/input.xml"); 
     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     A a = (A) unmarshaller.unmarshal(xml); 

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

} 

入力/出力

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<a> 
    <b id="123"/> 
    <c b="123"/> 
</a> 
関連する問題