引起:org.hibernate.PropertyValueException:not-null属性引用null或transient值:

时间:2014-09-04 08:31:56

标签: web-services spring-mvc

这里是Bussinss对象类,其中pwdId和userId在db中不是空的

@Entity
@Table(name="CLOUD_SVR_PASSWORDS_HISTORY")
@NamedQuery(name="CloudSvrPasswordsHistory.findAll", query="SELECT c FROM CloudSvrPasswordsHistory c")
public class CloudSvrPasswordsHistory implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    @Column(name="PWD_ID",nullable=false)
    private long pwdId;

    @Column(name="OLD_PASSWORD")
    private String oldPassword;

    @Column(name="CURRENT_PASSWORD")
    private String currentPassword;

    @Column(name="PWD_CHANGE_TYPE")
    private String pwdChangeType;

    @Column(name="CREATED_DATE")
    private Timestamp createdDate;


    @ManyToOne
    @JoinColumn(name="USER_ID",nullable=false)
    private CloudSvrUser user;


    public long getPwdId() {
        return pwdId;
    }


    public void setPwdId(long pwdId) {
        this.pwdId = pwdId;
    }


    public String getOldPassword() {
        return oldPassword;
    }


    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }


    public String getCurrentPassword() {
        return currentPassword;
    }


    public void setCurrentPassword(String currentPassword) {
        this.currentPassword = currentPassword;
    }


    public String getPwdChangeType() {
        return pwdChangeType;
    }


    public void setPwdChangeType(String pwdChangeType) {
        this.pwdChangeType = pwdChangeType;
    }


    public Timestamp getCreatedDate() {
        return createdDate;
    }


    public void setCreatedDate(Timestamp createdDate) {
        this.createdDate = createdDate;
    }


    public CloudSvrUser getUser() {
        return user;
    }


    public void setUser(CloudSvrUser user) {
        this.user = user;
    }

这是我的服务实现类只有一个我指定的方法

@Transactional
public void changePassword(CloudSvrPasswordsHistory pwdInfo)throws BusinessException
{
    //String password=null;

    try{

        System.out.println("servcimpl----------");
        CloudSvrUser dbUser =getUser(pwdInfo);

        if(dbUser != null){

            List<CloudSvrPasswordsHistory> newPwdList  = new ArrayList<CloudSvrPasswordsHistory>();

            CloudSvrPasswordsHistory changedPwd = new CloudSvrPasswordsHistory();

            changedPwd.setOldPassword(pwdInfo.getOldPassword());
            changedPwd.setCurrentPassword(pwdInfo.getCurrentPassword());

            newPwdList.add(changedPwd);
            dbUser.setPassCode(changedPwd.getCurrentPassword());
            //set childs to parent
            pwdInfo.setUser(dbUser);

            dbUser.setUserPwdList(newPwdList);

            //password=
            changedPwd(dbUser);   
            System.out.println("serviceimplend---------");
        }
    }
    catch(DaoException daoexception)
    {
        throw new BusinessException(daoexception.getMessage());
    }
    //return password;   

}

这是我的DAOImpl类

@Repository("passwordDao")
public class PasswordDaoImpl extends BaseDaoImpl implements PasswordDao
{

    PasswordDaoImpl()
    {}

    public void ChangedPwd(CloudSvrUser user)
    {
        //String password=null;

        List<CloudSvrPasswordsHistory> pwdinfo = user.getUserPwdList();

        for(CloudSvrPasswordsHistory changedPwd:pwdinfo)
        {

            //changedPwd.setPwdId((new Long(1)));
            changedPwd.setCreatedDate(new Timestamp(System.currentTimeMillis()));
            changedPwd.setPwdChangeType("ByUser");

        }


        try{

            super.getHibernateTemplate().update(user);
            //this.userDao.updateUser(dbUser);

        }
        catch(DataAccessException accessException){

            throw new DaoException("Internal DB error occured.");

        }

        //return   password ;
    }

在控制台中提出请求获取异常时

  

引起:org.hibernate.PropertyValueException:not-null属性引用null或transient值:com.omnypay.dao.bo.CloudSvrPasswordsHistory.user

请帮帮我

1 个答案:

答案 0 :(得分:2)

该错误表示user实体的CloudSvrPasswordsHistory属性为null,而hibernate期望它为not-null,这是因为您告诉hibernate nullable=false user使用此映射的属性:

@ManyToOne
@JoinColumn(name="USER_ID",nullable=false)
private CloudSvrUser user;

因此,要解决此问题,您必须将user实体的CloudSvrPasswordsHistory属性设置为:

changedPwd.setUser(dbUser);