论坛首页 Java企业应用论坛

自定义Usertype不能更新的问题(特殊情况)

浏览 2122 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-11-16  
问题解决,是deepCopy方法没写好。

数据库是mysql5.0。
由于用户表有很多跟用户相关的数据,但是平时查询不作为条件使用,所以我设置了一个text字段Properties用于记录用户的一些信息,比如电话号码身份证等等。
public class ApplicationUserProperties implements Serializable
{
    private String identityName = null;

    private String identityCode = null;

    private boolean identityHidden = false;

    private boolean identityVeirified = false;

    private String phoneNumber = null;

    private boolean phoneNumberVerified = false;

    private boolean phoneNumberHidden = false;

    private String mobileNumber = null;

    private boolean mobileNumberVerified = false;

    private boolean mobileNumberHidden = false;

    /*---get and set methods--*/
}


写了一个自定义类型,用于把各个属性使用json方式存储,比如{"mobileNumber":1234567,“mobileNumber":false}
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import net.sf.json.JSONObject;

import org.hibernate.HibernateException;
import org.sothis.util.JsonUtils;

import com.bangyo.user.ApplicationUserProperties;

public class ApplicationUserPropertiesUserType extends AbstractMutableUserType
{
    private final static int[] SQL_TYPES =
    {
        Types.VARCHAR
    };

    public Object deepCopy(Object value) throws HibernateException
    {
        if (value==null)
            return null;

        return (ApplicationUserProperties)value;
    }

    public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
        throws HibernateException, SQLException
    {
        if (rs.wasNull())
            return null;

        String props = rs.getString(names[0]);

        if (null==props)
            return null;

        ApplicationUserProperties properties = new ApplicationUserProperties();
        JsonUtils.toBean(properties, JSONObject.fromString(props));
        return properties;
    }

    public void nullSafeSet(PreparedStatement st, Object value, int index)
        throws HibernateException, SQLException
    {
        if (value==null)
        {
            st.setNull(index, Types.VARCHAR);
        }
        else
        {
            ApplicationUserProperties userInfo = (ApplicationUserProperties)value;
            Object jsonObject = JsonUtils.fromBean(userInfo);
            st.setString(index, jsonObject.toString());
        }
    }

    public Class<?> returnedClass()
    {
        return ApplicationUserProperties.class;
    }

    public int[] sqlTypes()
    {
        return SQL_TYPES;
    }
}



自定义类型的父类:
import java.io.Serializable;

import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

public abstract class AbstractMutableUserType implements UserType
{
    public Object assemble(Serializable cached, Object owner)
        throws HibernateException
    {
        return deepCopy(cached);
    }

    public Serializable disassemble(Object value) throws HibernateException
    {
        return (Serializable)deepCopy(value);
    }

    public boolean equals(Object x, Object y) throws HibernateException
    {
        if (x==y)
            return true;
        if (x==null||y==null)
            return false;
        return x.equals(y);
    }

    public int hashCode(Object x) throws HibernateException
    {
        if (x==null)
            return 0;

        return x.hashCode();
    }

    public Object replace(Object original, Object target, Object owner)
        throws HibernateException
    {
        return deepCopy(original);
    }

    public boolean isMutable()
    {
        return true;
    }
}


ApplicationUser.hbm.xml中的引用
        <property name="properties" column="Properties"
            type="ApplicationUserPropertiesUserType"
            not-null="false" />


接着问题就是,insert操作的时候这个字段的值可以被插入,但是update操作的时候,这个值无论怎么设都无法更新,似乎是hibernate不知道这个值被更新了。
更新时的代码
        applicationUser.setGender(Gender.MALE);

        applicationUser.getProperties().setLastProductPoin(product.getPoin());
        applicationUser.getProperties().setLastProductTitle(product.getTitle());
        applicationUser.getProperties().setLastProductTagRootPoin(
            product.getTagRootPoin());
        applicationUser.getProperties()
            .setLastProductTagRootTitle(tagRootTitle);
        applicationUser.getProperties().setLastProductDateTime(
            "2007-01-01 11:00:00");
        applicationUserPersister.update(applicationUser);



其他类型可以,比如性别的,直接user.setGender(Gender.MALE),接着update就可以更新,可是上面那个就是不行,请大家帮我看看是为什么。谢谢
   发表时间:2007-11-16  
好像知道了,可能是equals和hashcode方法没override。
0 请登录后投票
   发表时间:2007-11-16  
suyejun 写道
好像知道了,可能是equals和hashcode方法没override。
两个方法覆盖了还是不行
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics