ConsignmentContainer.java

package com.tradecloud.domain.container;

import com.tradecloud.domain.model.ordermanagement.Consignment;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.hibernate.annotations.ForeignKey;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@Entity
@DiscriminatorValue("CONSIGNMENT")
@Access(AccessType.FIELD)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Container")
public class ConsignmentContainer extends Container {

    private static final long serialVersionUID = 1L;

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY)
    //@ManyToOne(fetch = FetchType.LAZY)
    @ForeignKey(name = "fk_consignment")
    @XmlTransient
    private Consignment consignment;

    /**
     * Constructors, one for normal use, one for hibernate.
     */
    public ConsignmentContainer(ContainerType containerType) {
        super(containerType);
    }

    public ConsignmentContainer() {
    }

    public ConsignmentContainer(ConsignmentContainer container) {
        setContainerType(container.getContainerType());
        setQuantity(container.getQuantity());
        setContainerDates(container.getContainerDates());
    }

    public Consignment getConsignment() {
        return consignment;
    }

    public void setConsignment(Consignment consignment) {
        this.consignment = consignment;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof ConsignmentContainer)) {
            return false;
        }
        ConsignmentContainer other = (ConsignmentContainer) obj;
        return new EqualsBuilder().appendSuper(super.equals(other)).append(consignment, other.consignment).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().appendSuper(super.hashCode()).append(consignment).toHashCode();
    }
}