Rob
Rob

Reputation: 21

Hibernate: UserType - nullSafeSet - column length

I'm defining my own UserType called FixedString and in nullSafeSet method I'd like to fill (rightPad) all strings with blanks (up to length of that column defined with annotation:

@Column(length=100, nullable=true)).

I have no problem to trim all strings in nullSafeGet method (with StringUtils.trim method):

public Object nullSafeGet(ResultSet inResultSet, String[] names, Object o) throws SQLException {
  String val = (String)Hibernate.STRING.nullSafeGet(inResultSet, names[0]);   
  return StringUtils.trim(val);
}

but I have no idea how I can get the length of current column (in my case COLUMN_LENGTH), so I can fill that string up with blanks (up to predefined 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)));
}

Could anyone help me with that pls? I'm open to any ideas...

Thanks a lot,

Rob

Upvotes: 2

Views: 1481

Answers (2)

Maarten
Maarten

Reputation: 21

Using inPreparedStatement.getParameterMetaData().getPrecision(i) as column length, will fix your problem.

Upvotes: 2

Firo
Firo

Reputation: 30813

the usertype has a property sqltype which returns a sql string type with specified length. AFAIK @Column(length=100 is ignored for non string types including usertypes

Upvotes: 0

Related Questions