Получение всех родительских данных в дочерней модели в ответ - PullRequest
0 голосов
/ 10 июня 2019

У меня есть две таблицы email_draft и email_draft_history.email_draft является родительской таблицей, и email_draft может иметь несколько email_draft_id.при попытке извлечь данные я получил все родительские данные и дочерние данные, но в дочерних данных я также получил родительские данные.

Сначала я попробовал fetch.lazy, но он дал мне ошибку, поэтому я должен пометить его как @JsonIgnore.Также я должен удалить ссылку на объект дочернего класса из tostring ().

   @Entity
   @NoArgsConstructor
   @AllArgsConstructor
   @Table(name = "email_draft", schema = "ssdapiadm")
   @JsonIgnoreProperties(ignoreUnknown = true)
   public class EmailDraft implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(generator = "email_draft_ed_id_seq", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "email_draft_ed_id_seq", sequenceName = "ssdapiadm.email_draft_ed_id_seq", allocationSize = 1)
@Column(name = "ed_id")
private Integer emailDraftId;

@Column(name = "email_to")
private String emailTo;

@Column(name = "cc")
private String cc;

@Column(name = "bcc")
private String bcc;

@Column(name = "subject")
private String subject;

@Column(name = "body")
@Lob
@Type(type = "org.hibernate.type.BinaryType")
private byte[] body;

@Column(name = "created_by")
private Integer createdBy;

@Column(name = "updated_by")
private Integer updatedBy;

@Column(name = "source")
private String source;

@Column(name = "created_date")
private LocalDateTime createdDate;

@Column(name = "updated_date")
private LocalDateTime updatedDate;

@Column(name = "draft_status")
private String draftStatus;

@Column(name = "client_ppl_code")
private Integer clientPplCode;

@Column(name = "assigned_to")
private Integer assignedTo;

@Column(name = "deleted_BY")
private Integer deletedBy;

@Column(name = "email_sent_by")
private Integer emailSentBy;

@Column(name = "deleted_date")
private LocalDateTime deletedDate;

@Column(name = "email_sent_date")
private LocalDateTime emailSentDate;

@Column(name = "is_joint_Consultation")
private Boolean isjointConsultation;

@Column(name = "comments")
private String comments;

@Column(name = "version_id")
private Integer versionId;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL,mappedBy="emailDraft")
@JsonIgnore
private List<Activity> activity;


public String getEmailTo() {
    return emailTo;
}

public void setEmailTo(String emailTo) {
    this.emailTo = emailTo;
}

public String getCc() {
    return cc;
}

public void setCc(String cc) {
    this.cc = cc;
}

public String getSubject() {
    return subject;
}

public void setSubject(String subject) {
    this.subject = subject;
}

public Integer getCreatedBy() {
    return createdBy;
}

public void setCreatedBy(Integer createdBy) {
    this.createdBy = createdBy;
}

public Integer getUpdatedBy() {
    return updatedBy;
}

public void setUpdatedBy(Integer updatedBy) {
    this.updatedBy = updatedBy;
}

public String getSource() {
    return source;
}

public void setSource(String source) {
    this.source = source;
}

public LocalDateTime getCreatedDate() {
    return createdDate;
}

public void setCreatedDate(LocalDateTime createdDate) {
    this.createdDate = createdDate;
}

public LocalDateTime getUpdatedDate() {
    return updatedDate;
}

public void setUpdatedDate(LocalDateTime updatedDate) {
    this.updatedDate = updatedDate;
}

public String getDraftStatus() {
    return draftStatus;
}

public void setDraftStatus(String draftStatus) {
    this.draftStatus = draftStatus;
}

public Integer getClientPplCode() {
    return clientPplCode;
}

public void setClientPplCode(Integer clientPplCode) {
    this.clientPplCode = clientPplCode;
}

public static long getSerialversionuid() {
    return serialVersionUID;
}

public String getBcc() {
    return bcc;
}

public void setBcc(String bcc) {
    this.bcc = bcc;
}

public Integer getAssignedTo() {
    return assignedTo;
}

public void setAssignedTo(Integer assignedTo) {
    this.assignedTo = assignedTo;
}

public byte[] getBody() {
    return body;
}

public void setBody(byte[] body) {
    this.body = body;
}

public Integer getDeletedBy() {
    return deletedBy;
}

public void setDeletedBy(Integer deletedBy) {
    this.deletedBy = deletedBy;
}

public Integer getEmailSentBy() {
    return emailSentBy;
}

public void setEmailSentBy(Integer emailSentBy) {
    this.emailSentBy = emailSentBy;
}

public LocalDateTime getDeletedDate() {
    return deletedDate;
}

public void setDeletedDate(LocalDateTime deletedDate) {
    this.deletedDate = deletedDate;
}

public LocalDateTime getEmailSentDate() {
    return emailSentDate;
}

public void setEmailSentDate(LocalDateTime emailSentDate) {
    this.emailSentDate = emailSentDate;
}

public String getComments() {
    return comments;
}

public void setComments(String comments) {
    this.comments = comments;
}

public Integer getEmailDraftId() {
    return emailDraftId;
}

public void setEmailDraftId(Integer emailDraftId) {
    this.emailDraftId = emailDraftId;
}

public Boolean getIsjointConsultation() {
    return isjointConsultation;
}

public void setIsjointConsultation(Boolean isjointConsultation) {
    this.isjointConsultation = isjointConsultation;
}

public Integer getVersionId() {
    return versionId;
}

public void setVersionId(Integer versionId) {
    this.versionId = versionId;
}

public List<Activity> getActivity() {
    return activity;
}

public void setActivity(List<Activity> activity) {
    this.activity = activity;
}

@Override
public String toString() {
    return "EmailDraft [emailDraftId=" + emailDraftId + ", emailTo=" + emailTo + ", cc=" + cc + ", bcc=" + bcc
            + ", subject=" + subject + ", body=" + Arrays.toString(body) + ", createdBy=" + createdBy
            + ", updatedBy=" + updatedBy + ", source=" + source + ", createdDate=" + createdDate + ", updatedDate="
            + updatedDate + ", draftStatus=" + draftStatus + ", clientPplCode=" + clientPplCode + ", assignedTo="
            + assignedTo + ", deletedBy=" + deletedBy + ", emailSentBy=" + emailSentBy + ", deletedDate="
            + deletedDate + ", emailSentDate=" + emailSentDate + ", isjointConsultation=" + isjointConsultation
            + ", comments=" + comments + ", versionId=" + versionId + "]";
}
    }


    @Entity
    @Table(name = "email_draft_activity", schema = "ssdapiadm")
    @JsonIgnoreProperties(ignoreUnknown = true)
    public class Activity implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(generator = "email_draft_activity_ed_activity_id_seq", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "email_draft_activity_ed_activity_id_seq", sequenceName = "ssdapiadm.email_draft_activity_ed_activity_id_seq", allocationSize = 1)
@Column(name = "ed_activity_id")
private Integer emaildraftActivityId;

@Column(name = "activity_type")
private String activityType;

@Column(name = "activity_name")
private String activityName;

@Column(name = "reason_code")
private String reasonCode;

@Column(name = "assigned_to")
private Integer assignedTo;

@ManyToOne(cascade = {CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH},optional=false,fetch=FetchType.EAGER)
@JoinColumn(name = "ed_id")
private EmailDraft emailDraft;

public static long getSerialversionuid() {
    return serialVersionUID;
}

public Integer getEmaildraftActivityId() {
    return emaildraftActivityId;
}

public String getActivityType() {
    return activityType;
}

public String getActivityName() {
    return activityName;
}

public String getReasonCode() {
    return reasonCode;
}

public Integer getAssignedTo() {
    return assignedTo;
}

public EmailDraft getEmailDraft() {
    return emailDraft;
}


public void setEmaildraftActivityId(Integer emaildraftActivityId) {
    this.emaildraftActivityId = emaildraftActivityId;
}

public void setActivityType(String activityType) {
    this.activityType = activityType;
}

public void setActivityName(String activityName) {
    this.activityName = activityName;
}

public void setReasonCode(String reasonCode) {
    this.reasonCode = reasonCode;
}

public void setAssignedTo(Integer assignedTo) {
    this.assignedTo = assignedTo;
}

public void setEmailDraft(EmailDraft emailDraft) {
    this.emailDraft = emailDraft;
}



@Override
public String toString() {
    return "Activity [emaildraftActivityId=" + emaildraftActivityId + ", activityType=" + activityType
            + ", activityName=" + activityName + ", reasonCode=" + reasonCode + ", assignedTo=" + assignedTo
            + ", emailDraft=" + emailDraft + ", emailDraftId=]";
}
    }

ниже мой класс обслуживания.: -

@Override
public EmailDraftModel fetchAllEmailDraftByEmailDraftId(Integer emailDraftid) throws Exception {
    EmailDraft draft = finadAllEmailDraft(emailDraftid);
    EmailDraftModel draftModel = null;
    if(draft != null){
        draftModel = new EmailDraftModel();
        convertEmailDraftToEmailDraftModel(draft, draftModel);
        checkAndSetNanoSecondsinEmailDraft(draftModel);
    }
    return draftModel;
}

private void convertEmailDraftToEmailDraftModel(EmailDraft draft, EmailDraftModel draftModel) {
    BeanUtils.copyProperties(draft, draftModel);
    if (!CollectionUtils.isEmpty(draft.getActivity())) {            draftModel.setActivity(draft.getActivity().stream().map(activity -> EntityMapperUtil.mapSourceObjectToTargetClass(activity, ActivityModel.class)).collect(Collectors.toList()));
    }
}

json, который я получаю в ответ: -

{
    "emailDraftId": 233,
    "emailTo": "something@something.com",
    "cc": "something@something.com",
    "bcc": "Assurance@labsomething.com>",
    "subject": "test",
    "body": "WWZEdGxVY0tRLnBuZyIgYWx0PSJpbWciIHdpZHRoPSI4MDAiIGhlaWdodD0iMzg2IiAvPjxiciAvPgo8ZGl2PiZuYnNwOzwvZGl2Pgo8L2Rpdj4KPC9ib2R5Pgo8L2h0bWw+",
    "createdBy": 12345,
    "updatedBy": 12345,
    "source": "postcall",
    "createdDate": "2019-06-06T08:52:13.103",
    "updatedDate": "2019-06-06T12:08:53.712",
    "draftStatus": "open",
    "clientPplCode": 12345,
    "assignedTo": null,
    "deletedBy": null,
    "emailSentBy": null,
    "deletedDate": null,
    "emailSentDate": null,
    "isDeleted": null,
    "isjointConsultation": false,
    "comments": "comment added",
    "activity": [{
        "emaildraftActivityId": 24,
        "activityType": "abcd",
        "activityName": "Saved Follow Up with Activity",
        "reasonCode": "Consultation - Phone",
        "assignedTo": 58426,
        "emailDraft": {
            "emailDraftId": 233,
            "emailTo": "something@something.com",
            "cc": "something@something.com",
            "bcc": "Assurance@something.com>",
            "subject": "test",
            "body": "2Pgo8L2Rpdj4KPC9ib2R5Pgo8L2h0bWw+",
            "createdBy": 12345,
            "updatedBy": 12345,
            "source": "rhyds",
            "createdDate": "2019-06-06T08:52:13.103",
            "updatedDate": "2019-06-06T12:08:53.712",
            "draftStatus": "open",
            "clientPplCode": 12345,
            "assignedTo": null,
            "deletedBy": null,
            "emailSentBy": null,
            "deletedDate": null,
            "emailSentDate": null,
            "isjointConsultation": false,
            "comments": "comment added",
            "versionId": 3
        },
        "emailDraftId": 3
    }, {
        "emaildraftActivityId": 25,
        "activityType": "abcd",
        "activityName": "Saved Follow Up with Activity",
        "reasonCode": "Consultation - Phone",
        "assignedTo": 58426,
        "emailDraft": {
            "emailDraftId": 233,
            "emailTo": "something@something.com",
            "cc": "something@something.com",
            "bcc": "Assurance@labsomething.com>",
            "subject": "test",
            "body": "PC9ib2R5Pgo8L2h0bWw+",
            "createdBy": 12345,
            "updatedBy": 12345,
            "source": "dfgjrfy",
            "createdDate": "2019-06-06T08:52:13.103",
            "updatedDate": "2019-06-06T12:08:53.712",
            "draftStatus": "open",
            "clientPplCode": 12345,
            "assignedTo": null,
            "deletedBy": null,
            "emailSentBy": null,
            "deletedDate": null,
            "emailSentDate": null,
            "isjointConsultation": false,
            "comments": "comment added",
            "versionId": 3
        },
        "emailDraftId": 3
    }]

}

Сначала я хочу получить родительские данные, а затем при необходимостиможет получить дочерние данные.И в дочерних данных я хочу получить только записи дочерних таблиц.

Я новичок в JPA, так что, думаю, я что-то здесь упустил.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...