java Hibernate 可以将空字符串默认为空字符串吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2638538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can Hibernate default a Null String to Empty String
提问by markthegrea
In our application we are pulling data from a DB2 mainframe database. If the database has "low values" in a field, hibernate sends a "null" value in the object. This occurs even if the column is defined as "not null".
在我们的应用程序中,我们从 DB2 大型机数据库中提取数据。如果数据库在某个字段中具有“低值”,则 hibernate 在该对象中发送一个“空”值。即使将列定义为“非空”,也会发生这种情况。
As we are doing XML parsing on this, Castor is having trouble with it. I would like to fix this in Hibernate. Also, all of the hibernate hbm files are generated, so we can't mess with them (they are regened from time to time.)
当我们对此进行 XML 解析时,Castor 遇到了麻烦。我想在 Hibernate 中解决这个问题。另外,所有的hibernate hbm文件都是生成的,所以我们不能乱用它们(它们会不时地重新生成。)
Any way to intercept all Strings and replace nulls with ""?
有""什么方法可以拦截所有字符串并用?
采纳答案by Pascal Thivent
- Create a custom user type to replace
nullwith"" - Put the user type name where you normally put a hibernate type name in
reveng.xml.
- 创建自定义用户类型来代替
null与"" - 将用户类型名称放在通常放置休眠类型名称的位置
reveng.xml。
回答by J. Chomel
Here is the Solution we (credits to @Betlistaand @markthegrea) came up with. Actually, this is an upper case one and is truncated (lots of abstract methods are removed).
这是我们(归功于@Betlista和@markthegrea)提出的解决方案。实际上,这是一个大写的并且被截断了(删除了许多抽象方法)。
public class UpperCaseUserType implements org.hibernate.usertype.UserType{
private static final int[] TYPES = {Types.VARCHAR};
public Object nullSafeGet(ResultSet resultSet, String[] strings, Object object) throws HibernateException, SQLException {
//This might be redundant to uppercase the getter...
return StringUtils.upperCase((String) Hibernate.STRING.nullSafeGet(resultSet, strings[0]));
}
public void nullSafeSet(PreparedStatement preparedStatement, Object object, int i) throws HibernateException, SQLException {
String string = StringUtils.upperCase((String) object);
Hibernate.STRING.nullSafeSet(preparedStatement, string, i);
}
}
And then you hook it up this way:
然后你把它连接起来:
<property name="partNumber" type="cat.dds.fpsdma.model.UpperCaseUserType">
<column name="PART_NUMBER" length="20" not-null="true" />
</property>
回答by J. Chomel
I solve same issue on my side by handling database NULL values in the connector setters, like advised here, with a simple
我通过在连接器设置器中处理数据库 NULL 值来解决同样的问题,就像这里建议的那样,使用一个简单的
/**
* Set the value related to the column: NOM
* @param nom the NOM value
*/
public void setNom (String nom) {
this.nom = (nom!= null) ? nom : "" ;
}
回答by GuruKulki
You can use hibernate interceptor which extends EmptyInterceptor to perform the operation you want to before actually firing that query.
您可以使用扩展 EmptyInterceptor 的休眠拦截器在实际触发该查询之前执行您想要的操作。
the example given here might help you
此处给出的示例可能对您有所帮助
http://www.mkyong.com/hibernate/hibernate-interceptor-example-audit-log/
http://www.mkyong.com/hibernate/hibernate-interceptor-example-audit-log/

