日期:2014-05-16  浏览次数:20426 次

[经验][hibernate][自定义类型]hibernate 3.6.3中自定义类型,json类型

首先感谢您百忙之中的阅读 :)

下面进入正题

?

在3.6.3中UserType已经过时了,所以直接实现接口Type

使用jackson json实现的,希望可以交流相关的实现。

对于月表hibernate是不是也能实现crud?期待您的交流

  1. 数据库:
    create table TEST (
    	id int,
    	json1 varchar(100),
    	json2 varchar(100)
    ) engine = innodb comment 'test';
    ?
  2. 类型定义:
    public class Test {
    	/** 标识 */
    	private Integer id;
    	private JsonNode json1;
    	private JsonNode json2;
    	...
    }
    重点在这个类JsonNodeType?
    public class JsonNodeType extends AbstractType {
    	private static final long serialVersionUID = 1L;
    	protected static transient ILocalLogger logger = LocalLoggerFactory.getLogger(JsonNodeType.class);
    
    	@Override
    	public int[] sqlTypes(Mapping mapping) {
    		return new int[] {StringType.INSTANCE.sqlType()};
    	}
    
    	@Override
    	public int getColumnSpan(Mapping mapping) {
    		return 1;
    	}
    
    	@Override
    	public Class<JsonNode> getReturnedClass() {
    		return JsonNode.class;
    	}
    
    	@Override
    	public boolean isDirty(Object oldState, Object currentState, boolean[] checkable, SessionImplementor session) {
    		return !isSame(oldState, currentState, session.getEntityMode());
    	}
    
    	@Override
    	public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws SQLException {
    		return nullSafeGet(rs, names[0], session, owner);
    	}
    
    	@Override
    	public Object nullSafeGet(ResultSet rs, String name, SessionImplementor session, Object owner) throws SQLException {
    		return toJsonNode(StringType.INSTANCE.nullSafeGet(rs, name, session));
    	}
    
    	@Override
    	public void nullSafeSet(PreparedStatement st, Object value, int index, boolean[] settable, SessionImplementor session) throws SQLException {
    		StringType.INSTANCE.nullSafeSet(st, toJsonStr(value), index, settable, session);
    	}
    
    	@Override
    	public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws SQLException {
    		StringType.INSTANCE.nullSafeSet(st, toJsonStr(value), index, session);
    	}
    
    	@Override
    	public void setToXMLNode(Node node, Object value, SessionFactoryImplementor factory) {
    		StringType.INSTANCE.setToXMLNode(node, toJsonStr(value), factory);
    	}
    
    	@Override
    	public String toLoggableString(Object value, SessionFactoryImplementor factory) {
    		return StringType.INSTANCE.toLoggableString(toJsonStr(value), factory);
    	}
    
    	@Override
    	public Object fromXMLNode(Node xml, Mapping factory) {
    		return StringType.INSTANCE.fromXMLNode(xml, factory);
    	}
    
    	@Override
    	public String getName() {
    		return "jsonNode";
    	}
    
    	@Override
    	public Object deepCopy(Object value, EntityMode entityMode, SessionFactoryImplementor factory) {
    		return value == null ? null : toJsonNode(value.toString());
    	}
    	
    	@Override
    	public boolean isMutable() {
    		return false;
    	}
    
    	@Override
    	public Object replace(Object original, Object target, SessionImplementor session, Object owner, @SuppressWarnings("rawtypes") Map copyCache) {
    		if (original == null) return null;
    		if (original.equals(target)) return target;
    		return toJsonNode(original.toString());
    	}
    
    	@Override
    	public boolean[] toColumnNullness(Object value, Mapping mapping) {
    		return StringType.INSTANCE.toColumnNullness(value, mapping);
    	}
    }
    
    
    /** json操作 */
    	public static final ObjectMapper JSON_MAPPER = new ObjectMapper();
    	static {
    		JSON_MAPPER.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false);
    		JSON_MAPPER.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
    		JSON_MAPPER.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
    	}
    	/**
    	 * json对象转化为字符串
    	 * @param value json对象
    	 * @return
    	 */
    	public static String toJsonStr(Object value) {
    		try {
    			return JSON_MAPPER.writeValueAsString(value);
    		} catch (IOException