当前位置: 首页 > 知识库问答 >
问题:

Hibernate error Not-null属性引用瞬态值-在当前操作之前必须保存瞬态实例

慕冠宇
2023-03-14

错误是:

Caused by : javax.el.ELException: /jsf/submit.xhtml @20,76 listener="#{BankLocationMB.saveLocation}": org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientPropertyValueException: 
Not-null property references a transient value - transient instance must be saved before current operation : bank.entity.BankLocation.bankFormat -> bank.entity.RefBankFormat; 
nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : 
bank.entity.BankLocation.bankFormat -> bank.entity.RefBankFormat
@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 = "BANK_FORMAT_ID", referencedColumnName = "BANK_FORMAT_ID")
    @ManyToOne(targetEntity=RefBankFormat.class, optional = false)
    private RefBankFormat bankFormat;

    public RefBankFormat getBankFormat() {
        return bankFormat;
    }
    public void setBankFormat(RefBankFormat bankFormat) {
        this.bankFormat = bankFormat;
    }
    
    @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_BANK_FORMAT", schema = "OWNR")
public class RefBankFormat implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "BANK_FORMAT_ID")
    private Integer bankFormatId;

    @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 RefBankFormat() {
    }

    public RefBankFormat(Integer bankFormatId) {
        this.bankFormatId = bankFormatId;
    }

    public RefBankFormat(Integer bankFormatId, String displayName) {
        this.bankFormatId = bankFormatId;
        this.displayName = displayName;
    }

    public Integer getbankFormatId() {
        return bankFormatId;
    }

    public void setbankFormatId(Integer bankFormatId) {
        this.bankFormatId = bankFormatId;
    }

    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 += (bankFormatId != null ? bankFormatId.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 RefBankFormat)) {
            return false;
        }
        RefBankFormat other = (RefBankFormat) object;
        if ((this.bankFormatId == null && other.bankFormatId != null) || (this.bankFormatId != null && !this.bankFormatId.equals(other.bankFormatId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "bank.entity.RefBankFormat[ bankFormatId=" + bankFormatId + " ]";
    }
    
}

有谁能给我出问题的地方提供解决方案吗?

共有1个答案

索瀚海
2023-03-14
@ManyToOne(targetEntity=RefBankFormat.class, optional = false)
private RefBankFormat bankFormat;

bankFormat被设置为强制(不是null),因此您必须首先将此对象保存到数据库中,然后才允许保存BankLocation对象

定义传递持久性的策略,也就是级联类型。

 类似资料: