0

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

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

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;
    }

}
4

1 回答 1

0

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

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

选项:

  • 您可以使用向模式派生类添加任意注释。所以将允许您添加@LazyCollection到目标属性。但是,您需要自定义要@LazyCollection出现的每个属性。
  • Hyperjaxb 支持所谓的变体(不同的生成模式)。例如,这是JPA 1.0 变体的配置,这是JPA 2.0 变体。您可以通过为 Hibernate 编写和配置一个新变体来扩展 Hyperjaxb,该变体将支持特定于 Hibernate 的注释和自定义。然而,这是相当先进的。我个人需要几天时间来实施。
于 2014-11-19T08:46:05.300 回答