问题相关的hibetnate获取“org.hibernate.PropertyValueException”not-null属性引用null

时间:2012-06-30 07:26:35

标签: java hibernate spring-mvc

我遇到org.hibernate.PropertyValueException问题:not-null属性引用null或transient值: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:not-null属性引用null或transient值: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>

1 个答案:

答案 0 :(得分:0)

首先,使您的映射看起来像the documentation中所述。一对一需要property-ref属性。

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