如何使用hibernate和Legacy数据库从char字段pojo中删除空格 [英] How to trim white spaces from char fields pojo using hibernate and Legacy database

查看:118
本文介绍了如何使用hibernate和Legacy数据库从char字段pojo中删除空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表的列为char(5),无法将其更改为varchar(5)。所以,当我使用hibernateTemplate从表中获取数值时,它会返回带有实际单一字母表值的空格(客户化的解决方法是使用.trim()方法检查NPE),但是我们是否提供了处理这种类型的方法的情况。
PS.I使用Spring支持hibernate dao支持。
(在SQL中,CHAR数据类型是一个固定长度的字符串,根据定义,其他字符用空格填充)。

解决方案

我已经提到了类似的问题在这里



在建议的解决方案中,我认为增加用户类型最佳套件的要求并使其更有意义,因为
1. @ PostLoad(或者实际上的生命周期方法)在使用SessionFactory和Hibernate时不起作用。
2.代码中的修改可以在代码重构中忽略,另一个开发者可能会忽略那些可能导致重写setter的setter。所以我使用下面的代码解。
1.有一个package-info.java: - 具有正在使用的typedefs的详细信息。
2.使用@Type注解字段(type =MyTrimTypeString)
3.定义用户类型类MyTrimTypeString,代码StepherSwensen的后续回复,nullSafeGet上的轻微更新看起来像

  @Override 
public Object nullSafeGet(ResultSet rs,String [] names,Object owner)throws HibernateException,SQLException
{
final String val = rs.getString(names [0]);
return(val == null?val:val.trim());
}

和nullSafeSet看起来像:

  @Override 
public void nullSafeSet(PreparedStatement st,Object value,int index)throws HibernateException,SQLException
{
final String val =(字符串值;
st.setString(index,val);
}


My table has column as char(5) and can not change it to varchar(5). So when I fetch values out from the table using hibernateTemplate , that returns added spaces with actual say single alphabet value.(A custome fix is to use .trim() method with checking NPE) but do we have a provided approach to handle this kind of situation. PS.I am using Spring support for hibernate dao support. (In SQL, the CHAR data type is a fixed length character string. By definition, the additional characters are padded wtih spaces.)

解决方案

I have referred discussion on similar question here

Of the suggested solutions I feel adding user type best suites the requirements and makes more sense because 1. @PostLoad (Or actually lifecycle methods) does not work with using SessionFactory with hibernate. 2. Modification in assessor could be ignored during code refractor and another developer may overlook the setters which may lead to overwriting the setter.

So I am using following solution. 1.Have one package-info.java :- This has details for typedefs being used. 2.Annotated the fields with @Type(type="MyTrimTypeString") 3.Defined a user type class MyTrimTypeString, Code for which followed reply by StepherSwensen with a slight update on nullSafeGet looks like

 @Override
  public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException
  {
    final String val = rs.getString(names[0]);
    return (val == null ? val : val.trim());
  }

and nullSafeSet looks like :

@Override
  public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException
  {
    final String val = (String)value;
    st.setString(index, val);
  }

这篇关于如何使用hibernate和Legacy数据库从char字段pojo中删除空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆