Hibernate version: 2.1.6
Mapping documents: <class name="Cat"> <id name="name" type="UStringType"> <generator class="assigned"/> </id> <version name="version"/> <property name="age"/> </class>
---- public class UString implements Serializable { private final String value; public UString (String value) { this.value = (value!=null ? value.toUpperCase() : null); } public String toString() { return value; } public boolean equals(Object o) { return ((UString)o).toString().equals(value); } public int hashCode() { return value.hashCode(); } }
------ public class UStringType implements UserType { private static final int[] SQL_TYPES = {Types.VARCHAR}; public int[] sqlTypes() { return SQL_TYPES; } public Class returnedClass() { return UString.class; } public boolean equals(Object x, Object y) { if (x==y) return true; if ((x==null) || (y==null)) return false; return x.equals(y); } public Object deepCopy(Object value) { return value; } public boolean isMutable() { return false; } public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException, SQLException { if (resultSet.wasNull()) return null; String value = resultSet.getString(names[0]); return new UString(value); } public void nullSafeSet(PreparedStatement statement, Object value, int index) throws HibernateException, SQLException { if (value==null) { statement.setNull(index, Types.VARCHAR); } else { UString ustring = (UString)value; statement.setString(index, ustring.toString()); } }
}
Code between sessionFactory.openSession() and session.close(): List cats = session.find("from Cat as cat where cat.name like ?", new UString("FU%"), Hibernate.custom(UStringType.class)); Iterator iter = cats.iterator();
Name and version of the database you are using: MySQL 4
I perfectly know that there's always a great debate on custom types (UerType and CompositeUserType).
I've read all the documentation (quite carefully, I suppose...) and a large number of posts in the forum, but...
1) I still don't understand if it's legal to have a mapping similar to that I've
posted: it seems to work perfectly but, who knows, ...
and
2) trying to query with a partial match, similar to the example:
SELECT * FROM cat WHERE name LIKE 'FU%'
It works!!
Now: could someone answer me to 1 and 2 ?
Thank you
|