首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@LazyCollection(LazyCollectionOption.FALSE)在hyperjaxb?

@LazyCollection(LazyCollectionOption.FALSE)在hyperjaxb?
EN

Stack Overflow用户
提问于 2014-11-18 00:24:26
回答 1查看 764关注 0票数 0

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

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

xml看起来像:

代码语言:javascript
复制
<ab>
    <cd>Some thing</cd>
    <cd>Another thing</cd>
</ab>

或者:

代码语言:javascript
复制
<ab>
    <ef>Some thing</ef>
    <ef>Another thing</ef>
</ab>

xsd如下所示:

代码语言:javascript
复制
<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>

生成的实体应该如下所示:

代码语言:javascript
复制
@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;
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-19 08:46:05

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

Hyperjaxb只支持标准的JPA1.0和2.0注释,因此不支持@LazyCollection

奥顿:

  • 可以使用jaxb2 2-注释-插件向模式派生类添加任意注释。因此,jaxb2 2-注释-插件将允许您将@LazyCollection添加到目标属性。但是,您需要自定义希望@LazyCollection出现在上面的每个属性。
  • Hyperjaxb支持所谓的变体(不同的生成模式)。例如,这是JPA1.0变体的配置,这是JPA2.0变体。您可以通过编写和配置Hibernate的新变体来扩展Hyperjaxb,这将支持Hibernate特定的注释和自定义。然而,这是相当先进的。我要花几天才能实施。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26984496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档