DaveSegal wrote:
Holger,
Did you have to modify your java class to make your custom usertype work? I find references to interface org.hibernate.usertype.UserType in Hibernate in Action, but the online html doc reads as though this can all be mapped in xml without modifying the java source.
Do I need to implement the UserType interface to make my custom class work, or can it all be done declaratively through the hbm file?
Thanks,
Dave
(I am using custom usertypes just for the purpose of strong typing throughout my applications. My classes aren't much more than wrappers around the java wrapper classes - nothing fancy.)
I used the usertype from here:
http://forum.hibernate.org/viewtopic.ph ... mmedstring
and extended it for hibernate3. No, I did not modify my java class and I don't know if there is a way to do this only declaratively through xml-mapping. Maybe anybody else knows this ?
I use this usertype:
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
public class TrimmedString implements UserType {
/**
* No arg-constructor.
* Creates a TrimmedString object.
*/
public TrimmedString() {
super();
}
/**
* @see org.hibernate.usertype.UserType#sqlTypes()
*/
public int[] sqlTypes() {
return new int[] { Types.CHAR };
}
/**
* @see org.hibernate.usertype.UserType#returnedClass()
*/
public Class returnedClass() {
return String.class;
}
/**
* @see org.hibernate.usertype.UserType#equals(java.lang.Object, java.lang.Object)
*/
public boolean equals(final Object x, final Object y) throws HibernateException {
return (x == y) || (x != null && y != null && (x.equals(y)));
}
/**
* @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object)
*/
public Object nullSafeGet(final ResultSet rs, final String[] names, final Object owner) throws HibernateException, SQLException {
String val = rs.getString(names[0]);
if (null == val) {
return null;
}
return val.trim();
}
/**
* @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)
*/
public void nullSafeSet(final PreparedStatement st, final Object value, final int index) throws HibernateException, SQLException {
st.setString(index, (String) value);
}
/**
* @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
*/
public Object deepCopy(final Object value) throws HibernateException {
if (value == null) {
return null;
}
return new String((String) value);
}
/**
* @see org.hibernate.usertype.UserType#isMutable()
*/
public boolean isMutable() {
return false;
}
/**
* @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
*/
public int hashCode(final Object x) throws HibernateException {
return x.hashCode();
}
/**
* @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
*/
public Serializable disassemble(final Object value) throws HibernateException {
return (Serializable) value;
}
/**
* @see org.hibernate.usertype.UserType#assemble(java.io.Serializable, java.lang.Object)
*/
public Object assemble(final Serializable cached, final Object owner) throws HibernateException {
return cached;
}
/**
* @see org.hibernate.usertype.UserType#replace(java.lang.Object, java.lang.Object, java.lang.Object)
*/
public Object replace(final Object original, final Object target, final Object owner) throws HibernateException {
return original;
}
}
In my mapping:
<typedef class="hibernate3.usertype.TrimmedString" name="trimmed_string" /
For example:
<property name="beschreibung" column="BESCHREIBUNG" type="trimmed_string" length="25"/>