对象引用未保存的瞬态实例?

Posted

技术标签:

【中文标题】对象引用未保存的瞬态实例?【英文标题】:object references an unsaved transient instance? 【发布时间】:2016-06-02 05:01:58 【问题描述】:

我有一个包含两个表 Batchstatistics 和 ReconProcessDateTracker 的数据库。我想在他们之间建立关系。我使用以下模型类使用休眠来实现这一点:

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;

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;

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

【问题讨论】:

我还有两个错误行,即 org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:249) at org.hibernate.type.EntityType.getIdentifier(EntityType.java:456 ) 也收到错误原因:java.sql.SQLException:INSERT 语句与 FOREIGN KEY 约束“FK_TblReconProcessDateTracker_TblBatchStatistics”冲突。冲突发生在数据库“Recon_KS_Test”、表“dbo.TblBatchStatistics”、列“BatchStatisticsId”中。 【参考方案1】:

TransientObjectException 将始终被抛出如果您的实体 TblReconProcessDateTracker 与之前尚未持久化的 TblBatchStatistics 保持一致。

为了解决你的问题,你要么

    在尝试保存 TblReconProcessDateTrackerinstance 之前,请确保您已持久化 TblBatchStatistics 实例。

    使用cascade = CascaseType.PERSIST, CascadeType.MERGE 设置关联级联。级联还有其他值,您可以找到here。 这基本上告诉 Hibernate,当它尝试对 TblReconProcessDateTracker 执行这两个操作中的任何一个时,持久/合并 TblBatchStatistics 的状态是可以接受的。

【讨论】:

以上是关于对象引用未保存的瞬态实例?的主要内容,如果未能解决你的问题,请参考以下文章

对象引用未保存的瞬态实例:在刷新之前保存瞬态实例[重复]

对象引用未保存的瞬态实例?

TransientObjectException - 对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例

TransientPropertyValueException:对象引用了未保存的瞬态实例 - 在刷新之前保存瞬态实例

数据未保存:对象引用了未保存的瞬态实例 - 在刷新之前保存瞬态实例 [重复]

对象引用未保存的瞬态实例 - 在刷新休眠 JPA 之前保存瞬态实例 [重复]