One way is to use a Hibernate custom UserType.
A rough example (some methods from the interface are left out for readability):
package org.example;
public class SubstringUserType implements UserType {
@Override
public Class<String> returnedClass() {
return String.class;
}
@Override
public Object nullSafeGet(final ResultSet rs, final String[] names, final SessionImplementor session, final Object owner) throws HibernateException, SQLException {
if (rs != null || names.length != 0 || null != names[0]) {
String desc = rs.getString(names[0]);
return desc.substring(0, 3);
}
}
}
and use the following annotation on the field.
@Type(type = "org.example.SubstringUserType")