@LazyCollection(LazyCollectionOption.FALSE) in hyperjaxb?

828 views Asked by At

How do I set a collection to be @LazyCollection(LazyCollectionOption.FALSE) in hyperjaxb?

Here is the example: I have an xml node ab which can contain either a list of subnodes of type cd or a list of subnodes of type ef. Both cd and ef only contain textual/string content. I have an xsd definition of this which I run through JAXB and hyperjaxb to create java classes with hibernate annotations complete with database tables. Instead of setting fetchtype, how do I get hyperjaxb to set @LazyCollection(LazyCollectionOption.FALSE) for each collection?

The xml looks like:

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

or:

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

The xsd looks like:

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

The resulting entity should look like:

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

}
1

There are 1 answers

0
lexicore On BEST ANSWER

@LazyCollection, or @org.hibernate.annotations.LazyCollection (in fully qualified form) is a proprietary Hibernate annotation, not JPA standard.

Hyperjaxb only supports standard JPA 1.0 and 2.0 annotations, so @LazyCollection is not supported.

Optons:

  • You can use to add arbitrary annotations to schema-derived classes. So will allow you to add @LazyCollection to the target properties. However, you will need to customize each and every property you want @LazyCollection to appear on.
  • Hyperjaxb supports so-called variants (different generation modes). For instance this is the configuration of the JPA 1.0 variant and this is the JPA 2.0 variant. You can extend Hyperjaxb by writing and configuring a new variant for Hibernate which would support Hibernate-specific annotations and customizations. However, this is rather advanced. Would take me personaly a few days to implement.