package eu.reply.lea.services.yard.entities;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.validation.constraints.NotNull;

import lombok.AccessLevel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;
import lombok.ToString;

import eu.reply.lea.services.masterdata.reference.DockReference;
import eu.reply.lea.services.yard.reference.LoadingUnitReference;
import eu.reply.sce.backend.core.entity.StringCustomizableEntity;
import eu.reply.sce.backend.core.entity.configuration.StringifyAttributes;

@Data
@ToString(callSuper = false, of = { "loadingUnit", "type", "status" })
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@StringifyAttributes(attributes = { "loadingUnit", "type", "status" })
public class LoadingUnitActivity extends StringCustomizableEntity implements eu.reply.lea.services.yard.model.LoadingUnitActivity {

	private static final long serialVersionUID = -5280893186599984457L;
    
    @ManyToOne
    private CreateMissionRequest createMissionRequest;

	@NonNull
	@NotNull
	private LoadingUnitReference loadingUnit;


	@NonNull
	@NotNull
	@Enumerated(EnumType.STRING)
	@Setter(value = AccessLevel.PRIVATE)
	private Type type;
	
	@AttributeOverrides({ @AttributeOverride(name = "businessKey", column = @Column(name = "dock_businessKey")),
        @AttributeOverride(name = "entityName", column = @Column(name = "dock_entityName")),
        @AttributeOverride(name = "module", column = @Column(name = "dock_module", length = 20)),
        @AttributeOverride(name = "entityId", column = @Column(name = "dock_entityId", length = 50)) })
	private DockReference dock;

	
	public LoadingUnitActivity(@NonNull final LoadingUnitReference loadingUnit, @NonNull final Type type) {
		this.loadingUnit = loadingUnit;
		this.type = type;
	}


}

package eu.reply.lea.services.yard.entities;


import java.util.Calendar;
import java.util.List;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.FlushModeType;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.NoResultException;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

import eu.reply.lea.services.masterdata.reference.LocationReference;
import eu.reply.lea.services.masterdata.reference.SiteReference;
import eu.reply.lea.services.yard.model.CreateMissionRequestStatus;
import eu.reply.sce.backend.core.entity.StandardEntity;

/**
 * The aim of this class is to save a new request to create an internal mission
 * from a specific Loading Unit
 */
@Data
@Table
@Entity
@Getter
@Setter
@EqualsAndHashCode(callSuper = false, exclude = {"loadingUnit", "createMissionRequestStatus", "from", "to"})
@NamedQueries
        ({
                @NamedQuery(name = CreateMissionRequest.Q_FIND_BY_ALL, query = "select c from CreateMissionRequest c where c.loadingUnit = :loadingUnit " +
                        "and c.from = :from and c.to = :to")
        })
public class CreateMissionRequest extends StandardEntity implements eu.reply.lea.services.yard.model.CreateMissionRequest {

    @Override
    public String resourceName() {
        return CreateMissionRequest.class.getSimpleName();
    }

    //region NamedQuery

    //region Q_FIND_BY_ALL
    public static final String Q_FIND_BY_ALL = "SendResourceRequest.findUsingAllParams";
    public static final String P_LOADING_UNIT = "loadingUnit";
    public static final String P_FROM = "from";
    public static final String P_TO = "to";
    //endregion

    //endregion

    //region Private Fields

    @OneToMany(fetch = FetchType.EAGER)
    private List<LoadingUnitActivity> loadingUnitActivity;

    /**
     * Loading Unit that request a new internal sending
     * operation
     */
    @ManyToOne(optional = false)
    private LoadingUnitResource loadingUnit;

    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name = "entityName", column = @Column(name = "site_entityName")),
        @AttributeOverride(name = "businessKey", column = @Column(name = "site_businesskey")),
        @AttributeOverride(name = "module", column = @Column(name = "site_module", length = 20)),
        @AttributeOverride(name = "entityId", column = @Column(name = "site_entityId", length = 50)) })
    private SiteReference siteReference;


    /**
     * Get SendRequestStatus.
     */
    @Enumerated(EnumType.STRING)
    private CreateMissionRequestStatus createMissionRequestStatus;


    /**
     * Get error when working on this request.
     */
    private String error;


    /**
     * Location from the LU needs to move (start location)
     */
    @Embedded
    @AttributeOverrides(
            {@AttributeOverride(name = "module", column = @Column(name = "from_module")),
                    @AttributeOverride(name = "entityName", column = @Column(name = "from_entityName")),
                    @AttributeOverride(name = "entityId", column = @Column(name = "from_entityId")),
                    @AttributeOverride(name = "businessKey", column = @Column(name = "from_businessKey"))})
    private LocationReference from;


    /**
     * Location to the LU needs to reach (end location)
     */
    @Embedded
    @AttributeOverrides(
            {@AttributeOverride(name = "module", column = @Column(name = "to_module")),
                    @AttributeOverride(name = "entityName", column = @Column(name = "to_entityName")),
                    @AttributeOverride(name = "entityId", column = @Column(name = "to_entityId")),
                    @AttributeOverride(name = "businessKey", column = @Column(name = "to_businessKey"))})
    private LocationReference to;


    /**
     * Date in which the request was closed.
     */
    @Temporal(TemporalType.TIMESTAMP)
    private Calendar completedDate;
    //endregion

    //region Constructors

    /**
     * Construct SendResourceRequest instance
     */
    public CreateMissionRequest() {

    }

    /**
     * Construct SendResourceRequest instance
     *
     * @param loadingUnit Loading Unit
     * @param from        Location From
     * @param to          Location To
     */
    public CreateMissionRequest(@NotNull final LoadingUnitResource loadingUnit,
                                @NotNull final LocationReference from,
                                @NotNull final LocationReference to,
                                @NotNull final SiteReference siteReference){
        this.loadingUnit = loadingUnit;
        this.from = from;
        this.to = to;
        this.createMissionRequestStatus = CreateMissionRequestStatus.NEW;
        this.siteReference=siteReference;

    }
    //endregion

    //region findSendResourceRequest

    /**
     * Get SendResourceRequest
     *
     * @param entityManager EntityManageer
     * @param loadingUnit Loading Unit to search
     * @param from Location from of Loading unit
     * @param to Location to of Loading unit
     * @return retrieved SendResourceRequest
     */
    public static CreateMissionRequest findSendResourceRequest(@NotNull final EntityManager entityManager,
                                                               @NotNull final LoadingUnitResource loadingUnit,
                                                               @NotNull final LocationReference from,
                                                               @NotNull final LocationReference to) {
        CreateMissionRequest result = null;

        try {
            return entityManager.createNamedQuery(Q_FIND_BY_ALL, CreateMissionRequest.class)
                    .setParameter(P_LOADING_UNIT, loadingUnit)
                    .setParameter(P_FROM, from)
                    .setParameter(P_TO, to)
                    .setFlushMode(FlushModeType.COMMIT)
                    .getSingleResult();
        } catch (final NoResultException e) {
            result = null;
        }

        return result;
    }


    //endregion
}

It's missing the function to add loadingunitactivity to the createmisisonrequest