0

我面临 org.hibernate.PropertyValueException 的问题:非空属性引用空值或瞬态值:com.master.model.AccountLoan.account。有两个表Accout和AccountLoan,下面是Account的表结构

帐户 -

    create table ACCOUNT
    (
      ac_id               INTEGER not null,
      ac_name               VARCHAR2(40) not null,
      ac_islocked           CHAR(1) not null
    )

下面是账户贷款的表格结构

AccountLoan--

    create table ACCOUNT_LOAN
    (
      al_id            INTEGER not null,
      al_ac_id         INTEGER not null,
      al_loanA   NUMBER(15,2),
      al_loanB   NUMBER(15,2)
      )   

对于这两个表数据都是来自单个 jsp 的单个添加按钮单击。添加功能工作正常。账户贷款是可选的,如果用户不填写 LoanA 和 LoanB 字段,则账户贷款中没有记录,如果用户填写 LoanA 和 LoanB 字段,则记录插入到账户贷款表中。

目标

我想更新没有账户贷款的账户记录。当我更新账户记录时,抛出异常——org.hibernate.PropertyValueException:非空属性引用空值或瞬态值:com.master.model.AccountLoan.account。

下面是两个模型 java 文件。

import java.math.BigDecimal;

public class Account extends BaseM
{   
    private String name;            
    private Boolean isLocked;
    private AccountLoan accountLoan;

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
      this.name = name;
    }

    public Boolean getIsLocked()
    {
        return isLocked;
    }

    public void setIsLocked(Boolean isLocked)
    {
        this.isLocked = isLocked;
    }

    public AccountLoan getAccountLoan()
    {
        return accountLoan;
    }

    public void setAccountLoan(AccountLoan accountLoan)
    {
        this.accountLoan = accountLoan;
    }

}

Account Loan model java file

import java.math.BigDecimal;

public class AccountLoan extends BaseM
{
    private BigDecimal loanA;   
    private BigDecimal loanB;   
         private Account account;

    public BigDecimal getloanA()
    {
        return loanA;
    }


    public void setloanA(BigDecimal loanA)
    {
        this.loanA= loanA;
    }


    public BigDecimal getloanB()
    {
        return loanB;
    }


    public void setLoanInterest(BigDecimal loanB)
    {
        this.loanB= loanB;
    }

         public Account getAccount()
    {
        return account;
    }
    public void setAccount(Account account)
    {
        this.account = account;
    }

}


Account.hbm.xml

<hibernate-mapping>
    <class name="com.master.model.Account" table="ACCOUNT" dynamic-update="true">
        <id name="id" column="AC_ID" type="long">
            <generator class="com.common.support.IdGenerator">
                <param name="sequence">ACID_SEQ</param>
            </generator>
        </id>                    
       <one-to-one name="accountLoan" class="com.master.model.AccountLoan" cascade="all"/>       

      <property name="name" column="AC_NAME" type="string" />       
      <property name="isLocked" column="AC_ISLOCKED" type="yes_no" />
</class>
</hibernate-mapping>

AccountLoan.hbm.xml

<hibernate-mapping>

    <class name="com.master.model.AccountLoan" table="ACCOUNT_LOAN" dynamic-update="true">
        <id name="id" column="AL_ID" type="long">
            <generator class="com.common.support.IdGenerator">
                <param name="sequence">ALID_SEQ</param>
            </generator>
        </id>

       <many-to-one name="account" class="com.master.model.Account" unique="true">
           <column name="AL_AC_ID" not-null="true" />
       </many-to-one>    

       <property name="loanA" column="AL_LOANA" type="big_decimal" /> 
       <property name="loanB" column="AL_LOANB" type="big_decimal" />

    </class>
</hibernate-mapping>
4

1 回答 1

0

首先,使您的映射看起来像文档中描述的那样。一对一需要一个 property-ref 属性。

然后,确保AccountLoan.account在要将 AccountLoan 附加到 Account 时初始化属性。初始化Account.accountLoan字段是不够的。

于 2012-06-30T07:40:40.940 回答