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

问题解决,是deepCopy方法没写好。

数据库是mysql5.0。
由于用户表有很多跟用户相关的数据,但是平时查询不作为条件使用,所以我设置了一个text字段Properties用于记录用户的一些信息,比如电话号码身份证等等。
[code]
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--*/
}
[/code]

写了一个自定义类型,用于把各个属性使用json方式存储,比如{"mobileNumber":1234567,“mobileNumber":false}
[code]
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;
}
}

[/code]

自定义类型的父类:
[code]
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;
}
}
[/code]

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

接着问题就是,insert操作的时候这个字段的值可以被插入,但是update操作的时候,这个值无论怎么设都无法更新,似乎是hibernate不知道这个值被更新了。
更新时的代码
[code]
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);
[/code]


其他类型可以,比如性别的,直接user.setGender(Gender.MALE),接着update就可以更新,可是上面那个就是不行,请大家帮我看看是为什么。谢谢
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值