ExternalReferenceWrapper.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tradecloud.domain.common;

import com.tradecloud.common.base.PersistenceBase;
import com.tradecloud.common.externalreference.ExternalReference;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.HashSet;
import java.util.Set;

/**
 * Wrapper class implemented purely because of a nasty Hibernate bug in Hibernate 3.
 * <p>
 * https://hibernate.atlassian.net/browse/HHH-2763
 * <p>
 * I couldn't find any other way around it.
 * <p>
 * Blogged about it here: https://connect.devstream.net/pages/viewpage.action?pageId=11174611
 * <p>
 * Apologies if you're the person that has to fix this.
 * <p>
 * See IntegratedPersistenceBase.java for how I WANTED to implemente it.
 *
 * @author ronan
 */
@Entity
@Table(name = "externalreferencewrapper")
@Access(AccessType.FIELD)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "ExternalReferences")
public class ExternalReferenceWrapper extends PersistenceBase {

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
    @Fetch(FetchMode.SUBSELECT)
    Set<ExternalReference> externalReferences;

    public Set<ExternalReference> getExternalReferences() {
        if (externalReferences == null) {
            externalReferences = new HashSet<ExternalReference>();
        }
        return externalReferences;
    }

    public void setExternalReferences(Set<ExternalReference> references) {
        getExternalReferences().addAll(references);
    }

    public void addExternalReference(ExternalReference ref) {
        getExternalReferences().add(ref);
    }

    public void deleteExternalReference(ExternalReference ref) {
        getExternalReferences().remove(ref);
    }

}