Hibernate 对象引用未保存的临时实例?

Hibernate 对象引用未保存的临时实例?,hibernate,Hibernate,我有一个数据库,有两个表Batchstatistics和ReconProcessDateTracker。我想了解他们之间的关系。我使用以下模型类使用hibernate实现此功能: private String batchStatisticsId; private TblBatch tblBatch; private Date batchStatisticsStartDateTime; private Date batchStatisticsEndDateTime; private String

我有一个数据库,有两个表Batchstatistics和ReconProcessDateTracker。我想了解他们之间的关系。我使用以下模型类使用hibernate实现此功能:

private String batchStatisticsId;
private TblBatch tblBatch;
private Date batchStatisticsStartDateTime;
private Date batchStatisticsEndDateTime;
private String batchStatisticsNumRecordsRead;
private String batchStatisticsNumRecordsAccepted;
private String batchStatisticsNumRecordsRejected;
private String fileName;
private String userCreate;
private Date dateCreate;
private String userModif;
private Date dateModif;
private Set<TblRecordException> tblRecordExceptions = new HashSet<TblRecordException>(
        0);
private Set<TblPgTransactions> tblPgTransactionses = new HashSet<TblPgTransactions>(
        0);
private Set<TblExceptions> tblExceptionses = new HashSet<TblExceptions>(0);
private Set<TblTransactions> tblTransactionses = new HashSet<TblTransactions>(
        0);
private Set<TblKioskStudioTransactions> tblKioskStudioTransactionses = new HashSet<TblKioskStudioTransactions>(
        0);

private Set<TblBillerTransactions> tblBillerTransactionses = new HashSet<TblBillerTransactions>(
        0);
private Set<TblEstateExceptions> tblEstateExceptionses = new HashSet<TblEstateExceptions>(
        0);
private Set<TblEstateTransactionsRepository> tblEstateTransactionsesRepository = new HashSet<TblEstateTransactionsRepository>(
        0);
private Set<TblEstateTransactions> tblEstateTransactionses = new HashSet<TblEstateTransactions>(
        0);

public TblBatchStatistics() {
}

public TblBatchStatistics(String batchStatisticsId) {
    this.batchStatisticsId = batchStatisticsId;
}

public TblBatchStatistics(String batchStatisticsId, TblBatch tblBatch,
        Date batchStatisticsStartDateTime, Date batchStatisticsEndDateTime,
        String batchStatisticsNumRecordsRead,
        String batchStatisticsNumRecordsAccepted,
        String batchStatisticsNumRecordsRejected, String fileName,
        String userCreate, Date dateCreate, String userModif,
        Date dateModif, Set<TblRecordException> tblRecordExceptions,
        Set<TblPgTransactions> tblPgTransactionses,
        Set<TblExceptions> tblExceptionses,
        Set<TblTransactions> tblTransactionses,
        Set<TblKioskStudioTransactions> tblKioskStudioTransactionses,
        Set<TblEstateTransactions> tblEstateTransactionses,
        Set<TblBillerTransactions> tblBillerTransactionses,
        Set<TblEstateTransactionsRepository> tblEstateTransactionsesRepository,
        Set<TblEstateExceptions> tblEstateExceptionses) {
    this.batchStatisticsId = batchStatisticsId;
    this.tblBatch = tblBatch;
    this.batchStatisticsStartDateTime = batchStatisticsStartDateTime;
    this.batchStatisticsEndDateTime = batchStatisticsEndDateTime;
    this.batchStatisticsNumRecordsRead = batchStatisticsNumRecordsRead;
    this.batchStatisticsNumRecordsAccepted = batchStatisticsNumRecordsAccepted;
    this.batchStatisticsNumRecordsRejected = batchStatisticsNumRecordsRejected;
    this.fileName = fileName;
    this.userCreate = userCreate;
    this.dateCreate = dateCreate;
    this.userModif = userModif;
    this.dateModif = dateModif;
    this.tblRecordExceptions = tblRecordExceptions;
    this.tblPgTransactionses = tblPgTransactionses;
    this.tblExceptionses = tblExceptionses;
    this.tblTransactionses = tblTransactionses;
    this.tblKioskStudioTransactionses = tblKioskStudioTransactionses;
    this.tblEstateTransactionses = tblEstateTransactionses;
    this.tblBillerTransactionses = tblBillerTransactionses;
    this.tblEstateTransactionsesRepository = tblEstateTransactionsesRepository;
    this.tblEstateExceptionses = tblEstateExceptionses;
}

@Id
@Column(name = "BatchStatisticsId", unique = true, nullable = false, length = 30)
public String getBatchStatisticsId() {
    return this.batchStatisticsId;
}

public void setBatchStatisticsId(String batchStatisticsId) {
    this.batchStatisticsId = batchStatisticsId;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "BatchId")
public TblBatch getTblBatch() {
    return this.tblBatch;
}

public void setTblBatch(TblBatch tblBatch) {
    this.tblBatch = tblBatch;
}

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "BatchStatisticsStartDateTime", length = 23)
public Date getBatchStatisticsStartDateTime() {
    return this.batchStatisticsStartDateTime;
}

public void setBatchStatisticsStartDateTime(
        Date batchStatisticsStartDateTime) {
    this.batchStatisticsStartDateTime = batchStatisticsStartDateTime;
}

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "BatchStatisticsEndDateTime", length = 23)
public Date getBatchStatisticsEndDateTime() {
    return this.batchStatisticsEndDateTime;
}

public void setBatchStatisticsEndDateTime(Date batchStatisticsEndDateTime) {
    this.batchStatisticsEndDateTime = batchStatisticsEndDateTime;
}

@Column(name = "BatchStatisticsNumRecordsRead", length = 50)
public String getBatchStatisticsNumRecordsRead() {
    return this.batchStatisticsNumRecordsRead;
}

public void setBatchStatisticsNumRecordsRead(
        String batchStatisticsNumRecordsRead) {
    this.batchStatisticsNumRecordsRead = batchStatisticsNumRecordsRead;
}

@Column(name = "BatchStatisticsNumRecordsAccepted", length = 50)
public String getBatchStatisticsNumRecordsAccepted() {
    return this.batchStatisticsNumRecordsAccepted;
}

public void setBatchStatisticsNumRecordsAccepted(
        String batchStatisticsNumRecordsAccepted) {
    this.batchStatisticsNumRecordsAccepted = batchStatisticsNumRecordsAccepted;
}

@Column(name = "BatchStatisticsNumRecordsRejected", length = 50)
public String getBatchStatisticsNumRecordsRejected() {
    return this.batchStatisticsNumRecordsRejected;
}

public void setBatchStatisticsNumRecordsRejected(
        String batchStatisticsNumRecordsRejected) {
    this.batchStatisticsNumRecordsRejected = batchStatisticsNumRecordsRejected;
}

@Column(name = "FileName", length = 50)
public String getFileName() {
    return this.fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}

@Column(name = "UserCreate", length = 50)
public String getUserCreate() {
    return this.userCreate;
}

public void setUserCreate(String userCreate) {
    this.userCreate = userCreate;
}

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "DateCreate", length = 23)
public Date getDateCreate() {
    return this.dateCreate;
}

public void setDateCreate(Date dateCreate) {
    this.dateCreate = dateCreate;
}

@Column(name = "UserModif", length = 50)
public String getUserModif() {
    return this.userModif;
}

public void setUserModif(String userModif) {
    this.userModif = userModif;
}

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "DateModif", length = 23)
public Date getDateModif() {
    return this.dateModif;
}

public void setDateModif(Date dateModif) {
    this.dateModif = dateModif;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "tblBatchStatistics")
public Set<TblRecordException> getTblRecordExceptions() {
    return this.tblRecordExceptions;
}

public void setTblRecordExceptions(
        Set<TblRecordException> tblRecordExceptions) {
    this.tblRecordExceptions = tblRecordExceptions;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "tblBatchStatistics")
public Set<TblPgTransactions> getTblPgTransactionses() {
    return this.tblPgTransactionses;
}

public void setTblPgTransactionses(
        Set<TblPgTransactions> tblPgTransactionses) {
    this.tblPgTransactionses = tblPgTransactionses;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "tblBatchStatistics")
public Set<TblExceptions> getTblExceptionses() {
    return this.tblExceptionses;
}

public void setTblExceptionses(Set<TblExceptions> tblExceptionses) {
    this.tblExceptionses = tblExceptionses;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "tblBatchStatistics")
public Set<TblTransactions> getTblTransactionses() {
    return this.tblTransactionses;
}

public void setTblTransactionses(Set<TblTransactions> tblTransactionses) {
    this.tblTransactionses = tblTransactionses;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "tblBatchStatistics")
public Set<TblKioskStudioTransactions> getTblKioskStudioTransactionses() {
    return this.tblKioskStudioTransactionses;
}

public void setTblKioskStudioTransactionses(
        Set<TblKioskStudioTransactions> tblKioskStudioTransactionses) {
    this.tblKioskStudioTransactionses = tblKioskStudioTransactionses;
}


@OneToMany(fetch = FetchType.LAZY, mappedBy = "tblBatchStatistics")
public Set<TblEstateExceptions> getTblEstateExceptionses() {
    return this.tblEstateExceptionses;
}

public void setTblEstateExceptionses(
        Set<TblEstateExceptions> tblEstateExceptionses) {
    this.tblEstateExceptionses = tblEstateExceptionses;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "tblBatchStatistics")
public Set<TblEstateTransactionsRepository> getTblEstateTransactionsesRepository() {
    return this.tblEstateTransactionsesRepository;
}

public void setTblEstateTransactionsesRepository(
        Set<TblEstateTransactionsRepository> tblEstateTransactionsesRepository) {
    this.tblEstateTransactionsesRepository = tblEstateTransactionsesRepository;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "tblBatchStatistics")
public Set<TblEstateTransactions> getTblEstateTransactionses() {
    return this.tblEstateTransactionses;
}

public void setTblEstateTransactionses(
        Set<TblEstateTransactions> tblEstateTransactionses) {
    this.tblEstateTransactionses = tblEstateTransactionses;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "tblBatchStatistics")
public Set<TblBillerTransactions> getTblBillerTransactionses() {
    return this.tblBillerTransactionses;
}

public void setTblBillerTransactionses(
        Set<TblBillerTransactions> tblBillerTransactionses) {
    this.tblBillerTransactionses = tblBillerTransactionses;
}
}


当我试图保存它时,我得到的错误对象引用了一个未保存的临时实例-在刷新之前保存临时实例:com.usp.entity.TblBatchStatistics

如果实体
TblReconProcessDateTracker
使用
TblBatchStatistics
之前尚未持久化的

为了解决你的问题,你也可以

  • 在尝试保存
    TblReconProcessDateTracker
    实例之前,请确保已保存了
    TblBatchStatistics
    实例

  • 使用
    cascade={CascadeType.PERSIST,CascadeType.MERGE}
    设置关联级联。您还可以找到级联的其他值。 这基本上告诉Hibernate,当它尝试在
    TblReconProcessDateTracker
    上执行这两个操作时,可以持久化/合并
    TblBatchStatistics
    的状态


  • 我还有两行错误,即org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:249),org.hibernate.type.EntityType.getIdentifier(EntityType.java:456)也得到了由以下原因引起的错误:java.sql.SQLException:INSERT语句与外键约束冲突“FK_TblReconProcessDateTracker_TblBatchStatistics”。冲突发生在数据库“Recon_KS_Test”、表“dbo.TblBatchStatistics”、列“BatchStatisticsId”中。
    private String reconProcessDateTrackerId;
    private TblReconProcessMaster tblReconProcessMaster;
    private TblBatchStatistics tblBatchStatistics;
    private String fileDate;
    private String fileType;
    private String uploadDate;
    private String userCreate;
    private Date dateCreate;
    private String userModif;
    private Date dateModif;
    
    public TblReconProcessDateTracker() {
    }
    
    public TblReconProcessDateTracker(String reconProcessDateTrackerId,
            TblReconProcessMaster tblReconProcessMaster) {
        this.reconProcessDateTrackerId = reconProcessDateTrackerId;
        this.tblReconProcessMaster = tblReconProcessMaster;
    }
    
    public TblReconProcessDateTracker(String reconProcessDateTrackerId,
            TblReconProcessMaster tblReconProcessMaster,
            TblBatchStatistics tblBatchStatistics, String fileDate,
            String fileType, String uploadDate, String userCreate,
            Date dateCreate, String userModif, Date dateModif) {
        this.reconProcessDateTrackerId = reconProcessDateTrackerId;
        this.tblReconProcessMaster = tblReconProcessMaster;
        this.tblBatchStatistics = tblBatchStatistics;
        this.fileDate = fileDate;
        this.fileType = fileType;
        this.uploadDate = uploadDate;
        this.userCreate = userCreate;
        this.dateCreate = dateCreate;
        this.userModif = userModif;
        this.dateModif = dateModif;
    }
    
    @Id
    @Column(name = "ReconProcessDateTrackerId", unique = true, nullable = false, length = 10)
    public String getReconProcessDateTrackerId() {
        return this.reconProcessDateTrackerId;
    }
    
    public void setReconProcessDateTrackerId(String reconProcessDateTrackerId) {
        this.reconProcessDateTrackerId = reconProcessDateTrackerId;
    }
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ReconProcessId", nullable = false)
    public TblReconProcessMaster getTblReconProcessMaster() {
        return this.tblReconProcessMaster;
    }
    
    public void setTblReconProcessMaster(
            TblReconProcessMaster tblReconProcessMaster) {
        this.tblReconProcessMaster = tblReconProcessMaster;
    }
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "BatchStatisticsId")
    public TblBatchStatistics getTblBatchStatistics() {
        return this.tblBatchStatistics;
    }
    
    public void setTblBatchStatistics(TblBatchStatistics tblBatchStatistics) {
        this.tblBatchStatistics = tblBatchStatistics;
    }
    
    @Column(name = "FileDate", length = 10)
    public String getFileDate() {
        return this.fileDate;
    }
    
    public void setFileDate(String fileDate) {
        this.fileDate = fileDate;
    }
    
    @Column(name = "FileType", length = 10)
    public String getFileType() {
        return this.fileType;
    }
    
    public void setFileType(String fileType) {
        this.fileType = fileType;
    }
    
    @Column(name = "UploadDate", length = 10)
    public String getUploadDate() {
        return this.uploadDate;
    }
    
    public void setUploadDate(String uploadDate) {
        this.uploadDate = uploadDate;
    }
    
    @Column(name = "UserCreate", length = 50)
    public String getUserCreate() {
        return this.userCreate;
    }
    
    public void setUserCreate(String userCreate) {
        this.userCreate = userCreate;
    }
    
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "DateCreate", length = 23)
    public Date getDateCreate() {
        return this.dateCreate;
    }
    
    public void setDateCreate(Date dateCreate) {
        this.dateCreate = dateCreate;
    }
    
    @Column(name = "UserModif", length = 50)
    public String getUserModif() {
        return this.userModif;
    }
    
    public void setUserModif(String userModif) {
        this.userModif = userModif;
    }
    
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "DateModif", length = 23)
    public Date getDateModif() {
        return this.dateModif;
    }
    
    public void setDateModif(Date dateModif) {
        this.dateModif = dateModif;
    }