zoukankan      html  css  js  c++  java
  • Hibernate: 复合主键 (Composite Key) 如何兼容 NULL值

    现在如果我们的数据库表设计中用的是复合主键,而且复合主键中某个Key值允许为 NULL(通常维护项目的DataModel不允许修改)。这样的话,用Hibernate的<composite-key>去映射的时候,如果Hibernate如果找到某Row中的一个Key值为 NULL, 它会将整个Row映射为NULL。这样,NullPointerException会莫名其妙的抛出来.

    解决办法如下.

    创建一个 UserType 转换这个Key column的值.
    1. 取值的时候,将Null 值 转为 空字符串(当然,根据自己的业务需要,可以改为其他的替代的字符串.)

    2. 保存的时候,将空字符串 转换为 Null值. (这样,数据库的表的数据结构也不会受到影响)

    UserType 定义如下:

    代码
    public class KeySTRINGNull2BlankType implements UserType{
    final String _blankChar = "";

    public Object assemble(Serializable cached, Object owner)
    throws HibernateException {
    // TODO Auto-generated method stub
    return null;
    }

    public Object deepCopy(Object value) throws HibernateException {
    // TODO Auto-generated method stub
    String copy = new String(value.toString());
    return copy;
    }

    public Serializable disassemble(Object value) throws HibernateException {
    // TODO Auto-generated method stub
    return null;
    }

    public boolean equals(Object x, Object y) throws HibernateException {
    // TODO Auto-generated method stub

    if(x==y) return true;

    return EqualsBuilder().append(x, y).isEquals();
    }

    public int hashCode(Object x) throws HibernateException {
    // TODO Auto-generated method stub
    return new HashCodeBuilder().append(x).toHashCode();
    }

    public boolean isMutable() {
    // TODO Auto-generated method stub
    return false;
    }

    public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
    throws HibernateException, SQLException {
    // TODO Auto-generated method stub
    String variantName = (String) Hibernate.STRING.nullSafeGet(rs, names[0]);
    return variantName == null ? "" : variantName;
    }

    public void nullSafeSet(PreparedStatement st, Object value, int index)
    throws HibernateException, SQLException {
    // TODO Auto-generated method stub
    if( value.equals( _blankChar) ){
    Hibernate.STRING.nullSafeSet(st,
    null, index);
    }
    else{
    Hibernate.STRING.nullSafeSet(st, value, index);
    }

    }

    public Object replace(Object original, Object target, Object owner)
    throws HibernateException {
    // TODO Auto-generated method stub
    return null;
    }

    public Class returnedClass() {
    // TODO Auto-generated method stub
    return String.class;
    }

    public int[] sqlTypes() {
    // TODO Auto-generated method stub
    return new int[]{ Types.VARCHAR };
    }
    }

    配置文件如下定义 (sample):

    <composite-id>
    .......
    <key-property name="_field" column="FIELD" access="field" type="KeySTRINGNull2BlankType"/>
    .......
    </composite-id>
  • 相关阅读:
    [转]使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(三)-- Logger
    [转]ASP.NET Core配置环境变量和启动设置
    [转]ASP.NET Core 指定环境发布(hosting environment)
    [转]COPY OR MOVE FILES AND FOLDERS USING OLE AUTOMATION
    [转]How to Use xp_dirtree to List All Files in a Folder
    [转]How to nest transactions nicely
    [转]Using TRY...CATCH in Transact-SQL
    [转][C#] 对List<T>取交集、连集及差集
    [转]Pass a ViewBag instance to a HiddenFor field in Razor
    Apache Ignite简介以及Ignite和Coherence、Gemfire、Redis等的比较
  • 原文地址:https://www.cnblogs.com/skrifer/p/1847753.html
Copyright © 2011-2022 走看看