Update of /cvsroot/hibernate/Hibernate/cirrus/hibernate/type
In directory sc8-pr-cvs1:/tmp/cvs-serv22494/hibernate/type
Modified Files:
CalendarType.java
Added Files:
CalendarDateType.java
Log Message:
fixed a couple of bugs in multitable mappings
fixed an npe in CalendarType
--- NEW FILE: CalendarDateType.java ---
//$Id: CalendarDateType.java,v 1.1 2002/11/22 00:27:09 oneovthafew Exp $
package cirrus.hibernate.type;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Calendar;
import java.util.GregorianCalendar;
import cirrus.hibernate.HibernateException;
/**
* A type mapping for a <tt>Calendar</tt> object that represents a date.
*/
public class CalendarDateType extends MutableType {
/**
* @see cirrus.hibernate.type.NullableType#get(ResultSet, String)
*/
public Object get(ResultSet rs, String name)
throws HibernateException, SQLException {
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis( rs.getDate(name).getTime() );
return cal;
}
/**
* @see cirrus.hibernate.type.NullableType#set(PreparedStatement, Object, int)
*/
public void set(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException {
st.setDate( index, new Date( ( (Calendar) value ).getTimeInMillis() ) );
}
/**
* @see cirrus.hibernate.type.NullableType#sqlType()
*/
public int sqlType() {
return Types.DATE;
}
/**
* @see cirrus.hibernate.type.NullableType#toXML(Object)
*/
public String toXML(Object value) throws HibernateException {
//TODO:
return value.toString();
}
/**
* @see cirrus.hibernate.type.NullableType#deepCopyNotNull(Object)
*/
public Object deepCopyNotNull(Object value) throws HibernateException {
return ( (Calendar) value ).clone();
}
/**
* @see cirrus.hibernate.type.Type#returnedClass()
*/
public Class returnedClass() {
return Calendar.class;
}
/**
* @see cirrus.hibernate.type.Type#equals(Object, Object)
*/
public boolean equals(Object x, Object y) throws HibernateException {
if (x==y) return true;
if (x==null || y==null) return false;
Calendar calendar1 = (Calendar) x;
Calendar calendar2 = (Calendar) y;
return calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH)
&& calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)
&& calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR);
}
/**
* @see cirrus.hibernate.type.Type#getName()
*/
public String getName() {
return "calendar_date";
}
}
Index: CalendarType.java
===================================================================
RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/type/CalendarType.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** CalendarType.java 21 Nov 2002 09:05:03 -0000 1.1
--- CalendarType.java 22 Nov 2002 00:27:09 -0000 1.2
***************
*** 22,28 ****
public Object get(ResultSet rs, String name)
throws HibernateException, SQLException {
! Calendar cal = new GregorianCalendar();
! cal.setTimeInMillis( rs.getTimestamp(name).getTime() );
! return cal;
}
--- 22,36 ----
public Object get(ResultSet rs, String name)
throws HibernateException, SQLException {
!
! Timestamp ts = rs.getTimestamp(name);
! if (ts!=null) {
! Calendar cal = new GregorianCalendar();
! cal.setTimeInMillis( ts.getTime() );
! return cal;
! }
! else {
! return null;
! }
!
}
|