@LazyCollection(LazyCollectionOption.FALSE) in hyperjaxb?



如何在 hyperjaxb 中设置要@LazyCollection(LazyCollectionOption.FALSE)集合?

下面是示例:我有一个 xml 节点ab它可以包含 cd 类型的子节点列表或 ef 类型的子节点列表。 cdef都只包含文本/字符串内容。 我有一个xsd定义,我通过JAXB和hyperjaxb运行它来创建带有休眠注释和数据库表的java类。 而不是设置 fetchtype,如何让 hyperjaxb 为每个集合设置@LazyCollection(LazyCollectionOption.FALSE)

该 xml 如下所示:

<ab>
    <cd>Some thing</cd>
    <cd>Another thing</cd>
</ab>

或:

<ab>
    <ef>Some thing</ef>
    <ef>Another thing</ef>
</ab>

xsd 如下所示:

<xs:complexType name="Ab">
  <xs:sequence>
    <xs:element name="cd" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
    <xs:element name="ef" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
  </xs:sequence>
</xs:complexType>

生成的实体应如下所示:

@Entity(name = "Ab")
@Table(name = "AB")
@Inheritance(strategy = InheritanceType.JOINED)
public class Ab implements Equals, HashCode {
    protected List<String> cd;
    protected List<String> ef;
    @XmlAttribute(name = "Hjid")
    protected Long hjid;
    protected transient List<Ab.AbCdItem> cdItems;
    protected transient List<Ab.AbEfItem> efItems;
    @OneToMany(targetEntity = Ab.AbCdItem.class, cascade = {CascadeType.ALL})
    @JoinColumn(name = "CD_ITEMS_AB_HJID")
    @LazyCollection(LazyCollectionOption.FALSE)
    public List<Ab.AbCdItem> getCdItems() {
        if (this.cdItems == null) {
            this.cdItems = new ArrayList<Ab.AbCdItem>();
        }
        if (ItemUtils.shouldBeWrapped(this.cd)) {
            this.cd = ItemUtils.wrap(this.cd, this.cdItems, Ab.AbCdItem.class);
        }
        return this.cdItems;
    }
    @OneToMany(targetEntity = Ab.AbEfItem.class, cascade = {CascadeType.ALL})
    @JoinColumn(name = "EF_ITEMS_AB_HJID")
    @LazyCollection(LazyCollectionOption.FALSE)
    public List<Ab.AbEfItem> getEfItems() {
        if (this.efItems == null) {
            this.efItems = new ArrayList<Ab.AbEfItem>();
        }
        if (ItemUtils.shouldBeWrapped(this.ef)) {
            this.ef = ItemUtils.wrap(this.ef, this.efItems, Ab.AbEfItem.class);
        }
        return this.efItems;
    }
}

@LazyCollection@org.hibernate.annotations.LazyCollection(完全限定形式)是一个专有的Hibernate注释,而不是JPA标准。

Hyperjaxb 仅支持标准的 JPA 1.0 和 2.0 注释,因此不支持@LazyCollection

奥普顿:

  • 您可以使用 jaxb2-annotate-plugin 向模式派生类添加任意注释。因此,jaxb2-annotate-plugin 将允许您将@LazyCollection添加到目标属性中。但是,您需要自定义要显示@LazyCollection的每个属性。
  • Hyperjaxb支持所谓的变体(不同的生成模式)。例如,这是 JPA 1.0 变体的配置,这是 JPA 2.0 变体。您可以通过为 Hibernate 编写和配置一个新的变体来扩展 Hyperjaxb,该变体将支持 Hibernate 特定的注释和自定义。但是,这是相当先进的。我个人需要几天时间才能实施。

最新更新