2012-06-30 25 views
0

我面臨org.hibernate.PropertyValueException的問題:not-null屬性引用null或瞬態值:com.master.model.AccountLoan 。帳戶。 有兩個表戶頭和AccountLoan,下面是Account表結構問題相關hibetnate獲取「org.hibernate.PropertyValueException」非空屬性引用null

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。添加功能工作正常。賬戶貸款是可選的,如果用戶沒有填寫貸款和貸款字段,則沒有記錄在賬戶貸款中,如果用戶填寫貸款和貸款字段,則記錄插入到賬戶貸款表中。

目的

我想更新已不佔loan.When我更新賬戶記錄,賬戶記錄下的例外是throwing-- org.hibernate.PropertyValueException:非空屬性引用null或瞬時值: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> 

回答

0

首先,讓你的映射看起來像在the documentation中描述的那樣。一對一需要一個property-ref屬性。

然後,確保在您要將AccountLoan附加到帳戶時初始化AccountLoan.account屬性。初始化Account.accountLoan字段是不夠的。

相關問題