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

hibernate-object引用未保存的瞬态实例-在刷新之前保存瞬态实例

凌波峻
2023-03-14

错误是:by:javax.el.elexception://jsf/submit.xhtml@20,76 listener=“#{banklocationmb.savelocation}”对象引用了一个未保存的瞬态实例-在刷新之前保存瞬态实例

尝试添加级联持久化,我收到以下错误

org.springframework.orm.jpa.jpasystemexception:org.hibernate.id.identifiergenerationexception:在调用save():bank.entity.banklocation.refloan之前,必须手动分配该类的ID;嵌套异常是javax.persistence.persistenceException:org.hibernate.id.IdentifierGenerationException:在调用save():bank.entity.bankLocation.refloan之前,必须手动分配该类的ID


@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个答案

丌官飞章
2023-03-14

这是错误消息的重要部分:在调用save():bank.entity.banklocation.refloan之前,必须手动分配此类的ID

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)

 类似资料: