我正在定义自己的 UserType,称为“FixedString”,在 nullSafeSet 方法中,我想用空格填充(rightPad)所有字符串(最多为使用注释定义的列的长度:
@Column(length=100, nullable=true))。
我可以毫无问题地修剪 nullSafeGet 方法中的所有字符串(使用 StringUtils.trim 方法):
public Object nullSafeGet(ResultSet inResultSet, String[] names, Object o) throws SQLException {
String val = (String)Hibernate.STRING.nullSafeGet(inResultSet, names[0]);
return StringUtils.trim(val);
}
但我不知道如何获取当前列的长度(在我的例子中为COLUMN_LENGTH),因此我可以用空格填充该字符串(最多达到预定义的列长度)
public void nullSafeSet(PreparedStatement inPreparedStatement, Object o, int i) throws SQLException {
String val = (String)o;
inPreparedStatement.setString(i, StringUtils.rightPad(val, COLUMN_LENGTH)));
}
有人可以帮我吗? 我对任何想法持开放态度...
非常感谢,
抢
最佳答案
使用inPreparedStatement.getParameterMetaData().getPrecision(i)
作为列长度,将解决您的问题。
关于Hibernate:UserType - nullSafeSet - 列长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9293257/