强制hyperjaxb为OneToMany关系生成Set而不是List

时间:2015-12-15 22:03:30

标签: java hibernate jpa jaxb hyperjaxb

我正在使用hyperjaxb从XSD文件生成JAXB-JPA注释类。我有很多遭遇的Persona实体。这是自定义部分

    <bindings node="xsd:complexType[@name='PersonaType']">
        <bindings node=".//xsd:element[@name='id']">
            <hj:id>
                <orm:generated-value strategy="AUTO"/>
            </hj:id>
        </bindings>
        <bindings node=".//xsd:element[@name='encounters']">
            <hj:one-to-many name="encountersRel">
                <orm:join-table name="PERSONA_ENCOUNTER">
                    <orm:join-column name="PERSONA_ID"/>
                    <orm:inverse-join-column name="ENCOUNTER_ID"/>
                </orm:join-table>
            </hj:one-to-many>
        </bindings>
    </bindings>

到目前为止一切顺利。问题是,默认情况下,这会生成此映射

/**
 * Gets the value of the encounters property.
 * 
 * <p>
 * This accessor method returns a reference to the live list,
 * not a snapshot. Therefore any modification you make to the
 * returned list will be present inside the JAXB object.
 * This is why there is not a <CODE>set</CODE> method for the encounters property.
 * 
 * <p>
 * For example, to add a new item, do as follows:
 * <pre>
 *    getEncounters().add(newItem);
 * </pre>
 * 
 * 
 * <p>
 * Objects of the following type(s) are allowed in the list
 * {@link EncounterType }
 * 
 * 
 */
@OneToMany(targetEntity = EncounterType.class, cascade = {
    CascadeType.ALL
})
@JoinTable(name = "PERSONA_ENCOUNTER", joinColumns = {
    @JoinColumn(name = "PERSONA_ID")
}, inverseJoinColumns = {
    @JoinColumn(name = "ENCOUNTER_ID")
})
public List<EncounterType> getEncounters() {
    if (encounters == null) {
        encounters = new ArrayList<EncounterType>();
    }
    return this.encounters;
}

/**
 * 
 * 
 */
public void setEncounters(List<EncounterType> encounters) {
    this.encounters = encounters;
}

问题是我需要映射使用Set而不是List。如何使用自定义实现此目的?

1 个答案:

答案 0 :(得分:0)

此处Hyperjaxb的作者。

据我记忆,目前没有这样的功能。

我可以想象一个XJC插件可以做到这一点,但我现在还没有意识到这一点。

另见:

  

JAXB - Binding element to Set instead of List