2014-09-22 90 views
3

我有以下對象如何更改JAXB中XmlRootElement在對象上的取消名稱?

電影

@Entity 
@Table(name="film") 
@XmlRootElement(name = "film") 
public class Film implements Serializable { 

    @Id 
    @Column(name="id") 
    private String fbId; 

    @Column(name="title") 
    private String title; 

    @ManyToMany 
    @JoinTable(
     name="direction", 
     joinColumns={@JoinColumn(name="film", referencedColumnName="id")}, 
     inverseJoinColumns={@JoinColumn(name="person", referencedColumnName="id")}) 
    private Collection<Person> directors; 

    @OneToMany(cascade={CascadeType.ALL}, mappedBy="film") 
    @MapKey(name="character") 
    private Map<String, Performance> performances; 

    //GETTERS 
    @XmlAttribute(name = "fbId") 
    public String getFreebaseId() { 
     return this.fbId; 
    } 

    @XmlElementRefs({ 
     @XmlElementRef(name="director", type=Person.class) 
    }) 
    public Collection<Person> getDirectors() { 
     return this.directors; 
    } 

    @XmlAnyElement(lax=true) 
    public Collection<Performance> getPerformances() { 
     ArrayList performancesArray = new ArrayList<Performance>(); 
     for (Map.Entry<String, Performance> entry : this.performances.entrySet()) { 
      Performance value = entry.getValue(); 
      performancesArray.add(value); 
     } 
     return performancesArray; 
    } 

} 

@Entity 
@Table(name="person") 
public class Person implements Serializable { 

    @Id 
    @Column(name="id") 
    private String fbId; 

    @Column(name="first_name") 
    private String firstName; 

    @Column(name="last_name") 
    private String lastName; 

    @OneToMany(cascade={CascadeType.ALL}, mappedBy="actor") 
    @XmlTransient 
    private Collection<Performance> performances; 

    //GETTERS 
    @XmlAttribute(name = "fbId") 
    public String getFreebaseId() { 
     return this.fbId; 
    } 

    @XmlElement(name = "firstName") 
    public String getFirstName() { 
     return this.firstName; 
    } 

    @XmlElement(name = "lastName") 
    public String getLastName() { 
     return this.lastName; 
    } 

    @XmlTransient 
    public Collection<Performance> getPerformances() { 
     return this.performances; 
    } 

} 

每個膜具有一個(或多個)導演和表演其是 「人」。我不需要爲導演製作一個班級,但我會爲性能做一些,因爲他們有更多的信息。

性能

@Entity 
@Table(name="performance") 
@XmlRootElement(name = "performace") 
public class Performance implements Serializable { 

    @Id 
    @ManyToOne(fetch=FetchType.LAZY) 
    @JoinColumn(name="film") 
    private Film film; 

    @Id 
    @Column(name="film_character") 
    private String character; 

    @ManyToOne(fetch=FetchType.LAZY) 
    @JoinColumn(name="actor") 
    private Person actor; 

    //TO STRING 
    public String toString() { 
     return this.actor.toString() + " - " + this.character; 
    } 

    //GETTERS 
    @XmlTransient 
    public Film getFilm() { 
     return this.film; 
    } 

    @XmlElementRefs({ 
     @XmlElementRef(name = "firstName", type = Person.class), 
     @XmlElementRef(name = "lastName", type = Person.class) 
    }) 
    public Person getActor() { 
     return this.actor; 
    } 

    @XmlElement(name = "character") 
    public String getCharacter() { 
     return this.character; 
    } 

} 

當我運行此通過JAXB我得到這個:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<film fbId="1"> 
    <description>Nice film</description> 
    <person fbId="1"> 
     <firstName>Quentin</firstName> 
     <lastName>Tarantino</lastName> 
    </person> 
    <performace> 
     <person fbId="4"> 
      <firstName>Steve</firstName> 
      <lastName>Buscemi</lastName> 
     </person> 
     <character>Billy</character> 
    </performace> 
    <performace> 
     <person fbId="2"> 
      <firstName>Jhon</firstName> 
      <lastName>Travolta</lastName> 
     </person> 
     <character>Vincent</character> 
    </performace> 
    <performace> 
     <person fbId="3"> 
      <firstName>Samuel</firstName> 
      <lastName>L Jackson</lastName> 
     </person> 
     <character>Jules</character> 
    </performace> 
    <performace> 
     <person fbId="1"> 
      <firstName>Quentin</firstName> 
      <lastName>Tarantino</lastName> 
     </person> 
     <character>Jimmie</character> 
    </performace> 
    <title>Pulp Fiction</title> 
</film> 

有沒有辦法改變 「人」 的名字?爲了得到這樣的:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<film fbId="1"> 
    <description>Nice film</description> 
    <director fbId="1"> 
     <firstName>Quentin</firstName> 
     <lastName>Tarantino</lastName> 
    </director> 
    <performace fbId="4"> 
     <firstName>Steve</firstName> 
     <lastName>Buscemi</lastName> 
     <character>Billy</character> 
    </performace> 
    <performace fbId="2"> 
     <firstName>Jhon</firstName> 
     <lastName>Travolta</lastName> 
     <character>Vincent</character> 
    </performace> 
    <performace fbId="3"> 
     <firstName>Samuel</firstName> 
     <lastName>L Jackson</lastName> 
     <character>Jules</character> 
    </performace> 
    <performace fbId="1"> 
     <firstName>Quentin</firstName> 
     <lastName>Tarantino</lastName> 
     <character>Jimmie</character> 
    </performace> 
    <title>Pulp Fiction</title> 
</film> 

回答

2

選項:

  • Director擴展Person有自己@XmlRootElement
  • 使用JAXBElement<? extends Person>,而不是Person

的問題是,@XmlElementRef。名稱不適用於@XmlRootElement,請閱讀here

如果type()是JAXBElement.class,那麼namespace()和name()指向 帶有XmlElementDecl的工廠方法。 XML元素名稱是來自工廠方法的XmlElementDecl註釋的元素名稱 ,或者如果 其替換組中的元素(其爲頭元素) 已在XML文檔中替換,則元素名稱爲 XmlElementDecl在被替換的元素上。

如果type()不是JAXBElement.class,則XML元素名稱是靜態使用的類型的 註釋XmlRootElement將類型相關聯的 XML元素名稱。如果類型未帶有XmlElementDecl註釋 ,那麼它是一個錯誤。

如果type()不是JAXBElement.class,那麼這個值必須是「」。

順便說

@XmlElementRefs({ 
    @XmlElementRef(name = "firstName", type = Person.class), 
    @XmlElementRef(name = "lastName", type = Person.class) 
}) 

似乎沒有有效的給我。 @XmlElementRef不應該映射目標類的屬性。

+0

感謝您的「XmlElementRefs」我已經解決了這個問題。我已經解決了導演......謝謝 – 2014-09-22 17:23:14

相關問題