Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: hibernate3
I created a type TimestampType which is implemented from EnhancedUserType, as shown below in HibernateUTC class. But when I load the entity from database using session4j, I can get the xml result, but the method toXMLString() is not called. I want this function to convert the date type data to the format corresponding to user custom timezone. Could anybody help me solve this problem? Thanks a lot.
custom type define:
public abstract class HibernateUTC implements UserType {
public static final int[] SQL_TYPES = { Types.TIMESTAMP };
protected Class objectClass = Date.class;
private static Calendar sUTCCalendar = Calendar.getInstance();
static {
// set the timezone for the calendar to UTC (= GMT)
sUTCCalendar.setTimeZone(TimeZone.getTimeZone("GMT"));
}
public boolean equals(Object x, Object y) throws HibernateException {
return (x == null) ? (y == null) : x.equals(y);
}
public int hashCode(Object arg0) throws HibernateException {
return arg0.hashCode();
}
public boolean isMutable() {
return true;
}
public Class returnedClass() {
return objectClass;
}
public int[] sqlTypes() {
return SQL_TYPES;
}
public Object assemble(Serializable cached, Object arg1)
throws HibernateException {
return deepCopy(cached);
}
public Serializable disassemble(Object value) throws HibernateException {
return (Serializable) deepCopy(value);
}
public Object replace(Object original, Object arg1, Object arg2)
throws HibernateException {
return deepCopy(original);
}
/**
* Like a Hibernate timestamp, but using the UTC TimeZone (not the default
* TimeZone).
*/
public static class
TimestampType extends HibernateUTC implements EnhancedUserType{
/**
* @see net.sf.hibernate.UserType#deepCopy(java.lang.Object)
*/
public Object deepCopy(Object value) {
return (value == null) ? null : new java.sql.Timestamp(
((java.util.Date) value).getTime());
}
/**
* @see net.sf.hibernate.UserType#nullSafeGet(java.sql.ResultSet,
* java.lang.String[], java.lang.Object)
*/
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws SQLException {
return rs.getTimestamp(names[0], sUTCCalendar); //use sUTCCalendar to parse the date got from database
}
/**
* @see net.sf.hibernate.UserType#nullSafeSet(java.sql.PreparedStatement,
* java.lang.Object, int)
*/
public void nullSafeSet(PreparedStatement st, Object value, int index)
throws SQLException {
if (!(value instanceof java.util.Date))
value = deepCopy(value);
st.setTimestamp(index, new java.sql.Timestamp( ((java.util.Date)value).getTime()), sUTCCalendar);
}
public Object fromXMLString(String value) {
System.out.println("in fromXMLString,value:"+value);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
return new java.sql.Timestamp(df.parse(value).getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return value;
}
public String objectToSQLString(Object arg0) {
return null;
}
public String toXMLString(Object arg0) {
System.out.println("in toXMLString,value:"+arg0.toString());
//return arg0.toString();
return "abc";
}
}
}
Mapping documents:
<class name="com.chen.sp.DateTest"
table="chen_temp_datetest" >
<id name="id" column="id" node="@id" unsaved-value="0">
<generator class="native" />
</id>
<property name="updated" column="updated" node="updated"
type="com.chen.hibnt.HibernateUTC$TimestampType" />
<property name="created" column="created" node="created"
type="timestamp" />
<property name="active" column="active" node="active" />
<property name="deleted" column="deleted" node="deleted" />
<property name="userId" column="userid" node="userid"/>
</class>
Code between sessionFactory.openSession() and session.close():
Session session = sessionFactory.openSession();
Session dom4jsession = (Session) session
.getSession(org.hibernate.EntityMode.DOM4J);
System.out.println("id:"+id);
Element elm= (Element)dom4jsession.load(DateTest.class, new Long(id));
if(elm == null){
System.out.println("no record.");
}
else
System.out.println(elm.asXML());
/*result is:
<DateTest id="1"><updated>2008-10-29 09:44:57.307</updated><created>2008-10-29 09:44:57</created><active>true</active><deleted>false</deleted><userid>1</userid></DateTest>
*/
dom4jsession.close();
Full stack trace of any exception that occurs:
Name and version of the database you are using:sqlserver2005
The generated SQL (show_sql=true):select datetest0_.id as id0_0_, datetest0_.updated as updated0_0_, datetest0_.created as created0_0_, datetest0_.active as active0_0_, datetest0_.deleted as deleted0_0_, datetest0_.userid as userid0_0_ from chen_temp_datetest datetest0_ where datetest0_.id=?
Debug level Hibernate log excerpt:
Problems with Session and transaction handling?
Read this:
http://hibernate.org/42.html