[英]How to change the name of the XmlRootElement in JAXB deppending on the object?
我有以下物品
電影
@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>
選項:
Director
使用自己的@XmlRootElement
擴展Person
JAXBElement<? extends Person>
JAXBElement<? extends Person>
而不是Person
問題是,@ XmlElementRef.name不適用於@XmlRootElement
,請在此處閱讀:
如果type()是JAXBElement.class,則namespace()和name()指向帶有XmlElementDecl的工廠方法。 XML元素名稱是工廠方法的XmlElementDecl批注中的元素名稱,或者如果在XML文檔中替換了其替換組中的元素(其為head元素),則元素名稱來自替換的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
不應映射目標類的屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.