Home > Software design >  The primary key of the parent table is the primary key of the child table, How can I map it using jp
The primary key of the parent table is the primary key of the child table, How can I map it using jp

Time:10-26

This is the diagram:

enter image description here

References: enter image description here

My Entity classes are as follows:

BATCH_JOB_EXECUTION TABLE

package com.maxcom.interfact_services.entity;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Entity
@Table(name = "BATCH_JOB_EXECUTION")
public class BatchJobExecutionEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "JOB_EXECUTION_ID", nullable = false)
    private Long jobExecutionId;

    @Column(name = "VERSION")
    private Long version;

    @ManyToOne
    @JoinColumn(name = "JOB_INSTANCE_ID", nullable = false, updatable = false)
    private BatchJobInstanceEntity jobInstanceId;

    @Column(name = "CREATE_TIME")
    @Temporal(TemporalType.TIMESTAMP)
    private Date createTime;

    @Column(name = "START_TIME")
    @Temporal(TemporalType.TIMESTAMP)
    private Date startTime;

    @Column(name = "END_TIME")
    @Temporal(TemporalType.TIMESTAMP)
    private Date endTime;

    @Column(name = "STATUS")
    private String status;

    @Column(name = "EXIT_CODE")
    private String exitCode;

    @Column(name = "EXIT_MESSAGE")
    private String exitMessage;

    @Column(name = "LAST_UPDATED")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastUpdated;

    @Column(name = "JOB_CONFIGURATION_LOCATION")
    private String jobConfigurationLocation;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "batchJobExecutionEntity", orphanRemoval = true)
    private Set<BatchStepExecutionEntity> batchSteps;

    @JsonManagedReference
    @MapsId("jobExecutionId")
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "jobExecutionId", orphanRemoval = true)
    // @JsonIgnoreProperties("batchJobExecutionEntity")
    private List<BatchJobExecutionParamsEntity> batchJobParams = new ArrayList<>();

    public BatchJobExecutionEntity() {
    }

    public Long getJobExecutionId() {
        return jobExecutionId;
    }

    public void setJobExecutionId(Long jobExecutionId) {
        this.jobExecutionId = jobExecutionId;
    }

    public Long getVersion() {
        return version;
    }

    public void setVersion(Long version) {
        this.version = version;
    }

    @JsonBackReference
    public BatchJobInstanceEntity getJobInstanceId() {
        return jobInstanceId;
    }

    public void setJobInstanceId(BatchJobInstanceEntity jobInstanceId) {
        this.jobInstanceId = jobInstanceId;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getExitCode() {
        return exitCode;
    }

    public void setExitCode(String exitCode) {
        this.exitCode = exitCode;
    }

    public String getExitMessage() {
        return exitMessage;
    }

    public void setExitMessage(String exitMessage) {
        this.exitMessage = exitMessage;
    }

    public Date getLastUpdated() {
        return lastUpdated;
    }

    public void setLastUpdated(Date lastUpdated) {
        this.lastUpdated = lastUpdated;
    }

    public String getJobConfigurationLocation() {
        return jobConfigurationLocation;
    }

    public void setJobConfigurationLocation(String jobConfigurationLocation) {
        this.jobConfigurationLocation = jobConfigurationLocation;
    }

    @JsonManagedReference
    public Set<BatchStepExecutionEntity> getBatchSteps() {
        return batchSteps;
    }

    public void setBatchSteps(Set<BatchStepExecutionEntity> batchSteps) {
        this.batchSteps = batchSteps;
    }

    public List<BatchJobExecutionParamsEntity> getBatchJobParams() {
        return batchJobParams;
    }

    public void setBatchJobParams(List<BatchJobExecutionParamsEntity> batchJobParams) {
        this.batchJobParams = batchJobParams;
    }
}


BATCH_JOB_EXECUTION_PARAMS TABLE

package com.maxcom.interfact_services.entity;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

@Entity
@Table(name = "BATCH_JOB_EXECUTION_PARAMS")
public class BatchJobExecutionParamsEntity implements Serializable {

    @Id
    // @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "JOB_EXECUTION_ID", length = 20)
    private Long jobExecutionId;

    // @JsonBackReference
    // @ManyToOne(fetch = FetchType.LAZY)
    // @JoinColumn(name= "JOB_EXECUTION_ID", insertable = false, updatable = false)
    // @JsonIgnoreProperties("batchJobParams")
    // private BatchJobExecutionEntity batchJobExecutionEntity;

    @Column(name = "TYPE_CD", length = 6)
    private String typeCd;

    @Column(name = "KEY_NAME", length = 100)
    private String keyName;

    @Column(name = "STRING_VAL", length = 250)
    private String stringVal;

    @Column(name = "DATE_VAL")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateVal;

    @Column(name = "LONG_VAL")
    private Long longVal;

    @Column(name = "DOUBLE_VAL")
    private Double doubleVal;

    @Column(name = "IDENTIFYING", columnDefinition = "char(1)")
    private String identifying;

    public BatchJobExecutionParamsEntity() {
    }

    public Long getJobExecutionId() {
        return jobExecutionId;
    }

    public void setJobExecutionId(Long jobExecutionId) {
        this.jobExecutionId = jobExecutionId;
    }

    public String getTypeCd() {
        return typeCd;
    }

    public void setTypeCd(String typeCd) {
        this.typeCd = typeCd;
    }

    public String getKeyName() {
        return keyName;
    }

    public void setKeyName(String keyName) {
        this.keyName = keyName;
    }

    public String getStringVal() {
        return stringVal;
    }

    public void setStringVal(String stringVal) {
        this.stringVal = stringVal;
    }

    public Date getDateVal() {
        return dateVal;
    }

    public void setDateVal(Date dateVal) {
        this.dateVal = dateVal;
    }

    public Long getLongVal() {
        return longVal;
    }

    public void setLongVal(Long longVal) {
        this.longVal = longVal;
    }

    public Double getDoubleVal() {
        return doubleVal;
    }

    public void setDoubleVal(Double doubleVal) {
        this.doubleVal = doubleVal;
    }

    public String getIdentifying() {
        return identifying;
    }

    public void setIdentifying(String identifying) {
        this.identifying = identifying;
    }

    /*
    public BatchJobExecutionEntity getBatchJobExecutionEntity() {
        return batchJobExecutionEntity;
    }

    public void setBatchJobExecutionEntity(BatchJobExecutionEntity batchJobExecutionEntity) {
        this.batchJobExecutionEntity = batchJobExecutionEntity;
    }
     */

}


How to map 1:N of this scenario?

CodePudding user response:

Having a non-unique primary key is a recipe for disaster (/duplicates).
You missed enter image description here

The second picture shows more details:

enter image description here

Thank you all for your support.

Best regards

  • Related