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

Posted

技术标签:

【中文标题】HIbernate - 对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例【英文标题】:HIbernate - object references an unsaved transient instance - save the transient instance before flushing 【发布时间】:2021-03-03 07:29:06 【问题描述】:

HIbernate - 必须在调用 save 之前手动分配此类的 ID

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

我的 xhtml 页面中有一个下拉选项。 有时用户需要选择此下拉菜单, 有时他不需要选择下拉值。

我有两个相关实体,一个是主表,主表中的列(NULL)有一个引用表。 我正在尝试将数据保存在表格中。

错误是:引起:javax.el.E​​LException:/jsf/submit.xhtml @20,76 listener="#BankLocationMB.saveLocation" 对象引用了一个未保存的瞬态实例 - 在刷新之前保存瞬态实例

尝试添加级联 Persist 我收到以下错误

org.springframework.orm.jpa.JpaSystemException: org.hibernate.id.IdentifierGenerationException: 必须在调用 save() 之前手动分配此类的 id: bank.entity.BankLocation.RefLoan;嵌套异常是 javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: 这个类的 ids 必须在调用 save() 之前手动分配:bank.entity.BankLocation.RefLoan


@Entity
@Table(name = "BANK_LOCATION", schema = "OWNR")
public class BankLocation implements Serializable 

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "BANK_LOCATION_ID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "BANK_LOCATION_ID_SEQ")
    @SequenceGenerator(name = "BANK_LOCATION_ID_SEQ", sequenceName = "OWNR.BANK_LOCATION_ID_SEQ", allocationSize = 1)
    private Long bankLocationId;
    @Size(max = 32)
    @Column(name = "BANK_NAME")
    private String bankName;
    
    
    
    @JoinColumn(name = "LOAN_ID", referencedColumnName = "LOAN_ID", insertable = false, updatable = false)
    @ManyToOne(targetEntity=RefLoan.class, optional = true)
    private RefLoan loan;
    public RefLoan getLoan() 
        return loan;
    
    public void setLoan(RefLoan loan) 
        this.loan = loan;
    
    
    
    @Override
    public int hashCode() 
        int hash = 0;
        hash += (bankLocationId != null ? bankLocationId.hashCode() : 0);
        return hash;
    
    @Override
    public boolean equals(Object object) 
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof BankLocation)) 
            return false;
        
        BankLocation other = (BankLocation) object;
        if ((this.bankLocationId == null && other.bankLocationId != null) || (this.bankLocationId != null && !this.bankLocationId.equals(other.bankLocationId))) 
            return false;
        
        return true;
    
    @Override
    public String toString() 
        return "bank.entity.BankLocation[ bankLocationId=" + bankLocationId + " ]";
    
    



引用表实体


@Entity
@Table(name = "REF_LOAN", schema = "OWNR")

public class RefLoan implements Serializable 

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "LOAN_ID")
    private Integer loanId;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 50)
    @Column(name = "DISPLAY_NAME")
    private String displayName;
    @Size(max = 50)
    @Column(name = "DESCRIPTION")
    private String description;


    public RefLoan() 
    

    public RefLoan(Integer loanId) 
        this.loanId = loanId;
    

    public RefLoan(Integer loanId, String displayName) 
        this.loanId = loanId;
        this.displayName = displayName;
    

    public Integer getLoanId() 
        return loanId;
    

    public void setLoanId(Integer loanId) 
        this.loanId = loanId;
    

    public String getDisplayName() 
        return displayName;
    

    public void setDisplayName(String displayName) 
        this.displayName = displayName;
    

    public String getDescription() 
        return description;
    
    public void setDescription(String description) 
        this.description = description;
    


    @Override
    public int hashCode() 
        int hash = 0;
        hash += (loanId != null ? loanId.hashCode() : 0);
        return hash;
    

    @Override
    public boolean equals(Object object) 
        if (!(object instanceof RefLoan)) 
            return false;
        
        RefLoan other = (RefLoan) object;
        if ((this.loanId == null && other.loanId != null) || (this.loanId != null && !this.loanId.equals(other.loanId))) 
            return false;
        
        return true;
    

    @Override
    public String toString() 
        return "bank.entity.RefLoan[ loanId=" + loanId + " ]";
    
    


谁能提供我出错的解决方法?

谢谢

【问题讨论】:

【参考方案1】:

这是错误信息的重要部分:ids for this class must be manually assigned before calling save(): bank.entity.BankLocation.RefLoan

在您的 Refloan 类中,ID 未标记为自动生成:

@Id
@Basic(optional = false)
@NotNull
@Column(name = "LOAN_ID")
private Integer loanId;

您需要添加您在 BankLocation 类中使用的注释(在相应地调整序列名称之后)

@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "BANK_LOCATION_ID_SEQ")
@SequenceGenerator(name = "BANK_LOCATION_ID_SEQ", sequenceName = "OWNR.BANK_LOCATION_ID_SEQ", allocationSize = 1)

【讨论】:

【参考方案2】:

您可能有一些类似于以下的代码:

entityManager.merge(bankLocation);

您必须为贷款使用托管参考,如下所示:

if (bankLocation.getRefLoan() != null) 
    bankLocation.setRefLoan(entityManager.getReference(RefLoan.class, bankLocation.getRefLoan().getId());

entityManager.merge(bankLocation);

【讨论】:

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

Hibernate oneToMany - 对象引用一个未保存的瞬态实例

对象引用了一个未保存的瞬态实例 - Spring,JPA Hibernate

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

Grails Gorm:Object引用未保存的瞬态实例

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

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