Ok, problem solved: I had to switch from xdoclet1 to EJB annotations (which was quite a hard fight).
Here is the complete solution for all others that have this problem.
The UserDateType class:
Code:
package de.xxx.client.hibernate;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.usertype.ParameterizedType;
import org.hibernate.usertype.UserType;
/**
* Is used to fetch bigint values from the DB and to transform it into dates that contain
* milliseconds. MySQL would loose milliseconds when using a datetime field.
*
* Copied and adapted from http://hibernate.org/272.html
*/
public class DateUserType implements UserType, ParameterizedType {
private Class<?> clazz = null;
public void setParameterValues(Properties params) {
String myClassName = params.getProperty("myClassName");
if (myClassName == null) {
throw new MappingException("myClassName parameter not specified");
}
try {
this.clazz = Class.forName(myClassName);
} catch (java.lang.ClassNotFoundException e) {
throw new MappingException("Class " + myClassName + " not found", e);
}
}
private static final int[] SQL_TYPES = { Types.BIGINT };
public int[] sqlTypes() {
return SQL_TYPES;
}
public Class<?> returnedClass() {
return clazz;
}
public Date nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException, SQLException {
Date result = null;
if (!resultSet.wasNull()) {
long value = resultSet.getLong(names[0]);
result = new Date(value);
} else {
result = new Date(0);
}
return result;
}
public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException {
System.out.println("not implemented");
// if (value == null) {
// preparedStatement.setNull(index, Types.BIGINT);
// } else {
// preparedStatement.setString(index, value.name());
// }
}
public Object deepCopy(Object value) throws HibernateException {
return value;
}
public boolean isMutable() {
return false;
}
public Object assemble(Serializable cached, Object owner) throws HibernateException {
return cached;
}
public Serializable disassemble(Object value) throws HibernateException {
return (Serializable) value;
}
public Object replace(Object original, Object target, Object owner) throws HibernateException {
return original;
}
public int hashCode(Object x) throws HibernateException {
return x.hashCode();
}
public boolean equals(Object x, Object y) throws HibernateException {
if (x == y) return true;
if (null == x || null == y) return false;
return x.equals(y);
}
}
Part of the code from the bean class:
Code:
private Date evalDatetime = null;
/**
* @return the datetime on which the evaluation happened
*/
@Column(name="evalDatetime")
@Type(type = "de.xxx.client.hibernate.DateUserType",
parameters = { @Parameter(
name = "myClassName",
value = "de.xxx.client.hibernate.DateUserType") })
public Calendar getEvalDatetime() {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(evalDatetime);
return cal;
}
Configuration is done via addAnnotatedClass() (details:
http://www.hibernate.org/hib_docs/annot ... /ch01.html)
I hope this helps some other guys a lot of time =)