Hibernate version: 3.2.4.sp1
Mapping documents: Using XML
Name and version of the database you are using: Oracle XE/10G
Stemming from my earlier post, I did some further testing and found some interesting quirks
http://forum.hibernate.org/viewtopic.php?t=985706
Code:
cing.util.DateUserType] - < The date coming back 1196658000000>
2008-04-10 19:10:30,901 INFO [net.tp.FilterToHibernateMapper] - <Criteria info CriteriaImpl(net.tp.HibernateRequestObject:this[][])>
Hibernate: select * from ( select this_.request_id as request1_0_0_, this_.date_created as date2_0_0_, this_.division as division0_0_, this_.product_group as product4_0_0_, this_.sale_reporting_code as sale5_0_0_, this_.product_type as product6_0_0_, this_.territory as territory0_0_, this_.presentation as presenta8_0_0_, this_.unitId as unitId0_0_, this_.code as code0_0_, this_.name as name0_0_, this_.unit_typeid as unit12_0_0_, this_.countryTypeId as country13_0_0_, this_.country as country0_0_, this_.collectionId as collect15_0_0_, this_.col_code as col16_0_0_, this_.col_name as col17_0_0_, this_.col_componenttypeid as col18_0_0_, this_.cstateId as cstateId0_0_, this_.cs_code as cs20_0_0_, this_.cs_name as cs21_0_0_, this_.cs_shortcode as cs22_0_0_ from HIBERNATEREQUESTOBJECT2 this_ order by this_.request_id asc ) where rownum <= ?
2008-04-10 19:10:30,947 INFO [net.taxstream.transferpricing.util.DateUserType] - < The date coming back 1196658000000>
2008-04-10 19:10:30,947 INFO [net.tp.util.DateUserType] - < The date coming back 1196658000000>
2008-04-10 19:10:30,947 INFO [net.tp.DateUserType] - < The date coming back 1196658000000>
2008-04-10 19:10:30,947 INFO [net.tp.DateUserType] - < The date coming back 1196658000000>
2008-04-10 19:10:30,947 INFO [net.tp.DateUserType] - < The date coming back 1196658000000>
2008-04-10 19:10:31,026 INFO [net.tp.DateUserType] - < The date coming back 1196658000000>
2008-04-10 19:10:31,026 INFO [net.tp.DateUserType] - < The date coming back 1196658000000>
2008-04-10 19:10:31,026 INFO [net.tp.DateUserType] - < The date coming back 1196658000000>
2008-04-10 19:10:31,026 INFO [net.tp.DateUserType] - < The date coming back 1196658000000>
2008-04-10 19:10:31,026 INFO [net.tp.DateUserType] - < The date coming back 1196658000000>
2008-04-10 19:10:31,135 INFO [net.tp.DateUserType] - < The date coming back 1196658000000>
2008-04-10 19:10:31,135 INFO [net.tp.DateUserType] - < The date coming back 1196658000000>
2008-04-10 19:10:31,135 INFO [net.tp.DateUserType] - < The date coming back 1196658000000>
2008-04-10 19:10:31,166 INFO [net.tp.DateUserType] - < The date coming back 1196658000000>
2008-04-10 19:10:31,166 INFO [net.tp.DateUserType] - < The date coming back 1196658000000>
2008-04-10 19:10:31,229 INFO [net.tp.DateUserType] - < The date coming back 1196658000000>
2008-04-10 19:10:31,229 INFO [net.tp.DateUserType] - < The date coming back 1196658000000>
2008-04-10 19:10:31,229 INFO [net.tp.DateUserType] - < The date coming back 1196658000000>
2008-04-10 19:10:31,229 INFO [net.tp.DateUserType] - < The date coming back 1196658000000>
2008-04-10 19:10:31,229 INFO [net.tp.DateUserType] - < [u]The date coming back 1196658000000[/u]>
2008-04-10 19:11:20,369 INFO [net.tp.CriterionFactoryForFilterTypes] - <[u]From the criterion factory 1196658000000 and pattern yyyy-MM-dd[/u]>
Hibernate: select this_.request_id as request1_0_0_, this_.date_created as date2_0_0_, this_.division as division0_0_, this_.product_group as product4_0_0_, this_.sale_reporting_code as sale5_0_0_, this_.product_type as product6_0_0_, this_.territory as territory0_0_, this_.presentation as presenta8_0_0_, this_.unitId as unitId0_0_, this_.code as code0_0_, this_.name as name0_0_, this_.unit_typeid as unit12_0_0_, this_.countryTypeId as country13_0_0_, this_.country as country0_0_, this_.collectionId as collect15_0_0_, this_.col_code as col16_0_0_, this_.col_name as col17_0_0_, this_.col_componenttypeid as col18_0_0_, this_.cstateId as cstateId0_0_, this_.cs_code as cs20_0_0_, this_.cs_name as cs21_0_0_, this_.cs_shortcode as cs22_0_0_ from HIBERNATEREQUESTOBJECT2 this_ where this_.date_created=? and 1=1 order by this_.request_id asc
2008-04-10 19:11:20,447 INFO [net.tp.FilterToHibernateMapper] - <Criteria info CriteriaImpl(net.tp.HibernateRequestObject:this[][dateCreated=Mon Dec 03 00:00:00 EST 2007, ()])>
Hibernate: select * from ( select this_.request_id as request1_0_0_, this_.date_created as date2_0_0_, this_.division as division0_0_, this_.product_group as product4_0_0_, this_.sale_reporting_code as sale5_0_0_, this_.product_type as product6_0_0_, this_.territory as territory0_0_, this_.presentation as presenta8_0_0_, this_.unitId as unitId0_0_, this_.code as code0_0_, this_.name as name0_0_, this_.unit_typeid as unit12_0_0_, this_.countryTypeId as country13_0_0_, this_.country as country0_0_, this_.collectionId as collect15_0_0_, this_.col_code as col16_0_0_, this_.col_name as col17_0_0_, this_.col_componenttypeid as col18_0_0_, this_.cstateId as cstateId0_0_, this_.cs_code as cs20_0_0_, this_.cs_name as cs21_0_0_, this_.cs_shortcode as cs22_0_0_ from HIBERNATEREQUESTOBJECT2 this_ where this_.date_created=? and 1=1 order by this_.request_id asc ) where rownum <= ?
The number 1196658000000 is the date.getTime() for the data instances that I am trying to evaluate whether they are equal using Criterion.
<The date coming back 1196658000000>, is what is being pulled from the db when the resultset is returned.
<From the criterion factory 1196658000000 and pattern yyyy-MM-dd> this is what I am sending through via the creation of criterion; as you can see the getTime() of both objects are the same yet still it returns a zero resultset when I run this from the web app. In the bottom I show the same thing with actual dates
Code:
Hibernate: select this_.request_id as request1_0_0_, this_.date_created as date2_0_0_, this_.division as division0_0_, this_.product_group as product4_0_0_, this_.sale_reporting_code as sale5_0_0_, this_.product_type as product6_0_0_, this_.territory as territory0_0_, this_.presentation as presenta8_0_0_, this_.unitId as unitId0_0_, this_.code as code0_0_, this_.name as name0_0_, this_.unit_typeid as unit12_0_0_, this_.countryTypeId as country13_0_0_, this_.country as country0_0_, this_.collectionId as collect15_0_0_, this_.col_code as col16_0_0_, this_.col_name as col17_0_0_, this_.col_componenttypeid as col18_0_0_, this_.cstateId as cstateId0_0_, this_.cs_code as cs20_0_0_, this_.cs_name as cs21_0_0_, this_.cs_shortcode as cs22_0_0_ from HIBERNATEREQUESTOBJECT2 this_ where this_.date_created=? and 1=1 order by this_.request_id asc
2008-04-10 19:17:06,635 INFO [net.tp.FilterToHibernateMapper] - <Criteria info CriteriaImpl(net.tp.HibernateRequestObject:this[][dateCreated=Mon Dec 03 00:00:00 EST 2007, ()])>
Hibernate: select * from ( select this_.request_id as request1_0_0_, this_.date_created as date2_0_0_, this_.division as division0_0_, this_.product_group as product4_0_0_, this_.sale_reporting_code as sale5_0_0_, this_.product_type as product6_0_0_, this_.territory as territory0_0_, this_.presentation as presenta8_0_0_, this_.unitId as unitId0_0_, this_.code as code0_0_, this_.name as name0_0_, this_.unit_typeid as unit12_0_0_, this_.countryTypeId as country13_0_0_, this_.country as country0_0_, this_.collectionId as collect15_0_0_, this_.col_code as col16_0_0_, this_.col_name as col17_0_0_, this_.col_componenttypeid as col18_0_0_, this_.cstateId as cstateId0_0_, this_.cs_code as cs20_0_0_, this_.cs_name as cs21_0_0_, this_.cs_shortcode as cs22_0_0_ from HIBERNATEREQUESTOBJECT2 this_ where this_.date_created=? and 1=1 order by this_.request_id asc ) where rownum <= ?
Hibernate: select this_.request_id as request1_0_0_, this_.date_created as date2_0_0_, this_.division as division0_0_, this_.product_group as product4_0_0_, this_.sale_reporting_code as sale5_0_0_, this_.product_type as product6_0_0_, this_.territory as territory0_0_, this_.presentation as presenta8_0_0_, this_.unitId as unitId0_0_, this_.code as code0_0_, this_.name as name0_0_, this_.unit_typeid as unit12_0_0_, this_.countryTypeId as country13_0_0_, this_.country as country0_0_, this_.collectionId as collect15_0_0_, this_.col_code as col16_0_0_, this_.col_name as col17_0_0_, this_.col_componenttypeid as col18_0_0_, this_.cstateId as cstateId0_0_, this_.cs_code as cs20_0_0_, this_.cs_name as cs21_0_0_, this_.cs_shortcode as cs22_0_0_ from HIBERNATEREQUESTOBJECT2 this_ order by this_.request_id asc
2008-04-10 19:17:34,822 INFO [net.tp.DateUserType] - < The date coming back Mon Dec 03 00:00:00 EST 2007>
2008-04-10 19:17:34,838 INFO [net.tp.FilterToHibernateMapper] - <Criteria info CriteriaImpl(net.tp.HibernateRequestObject:this[][])>
Hibernate: select * from ( select this_.request_id as request1_0_0_, this_.date_created as date2_0_0_, this_.division as division0_0_, this_.product_group as product4_0_0_, this_.sale_reporting_code as sale5_0_0_, this_.product_type as product6_0_0_, this_.territory as territory0_0_, this_.presentation as presenta8_0_0_, this_.unitId as unitId0_0_, this_.code as code0_0_, this_.name as name0_0_, this_.unit_typeid as unit12_0_0_, this_.countryTypeId as country13_0_0_, this_.country as country0_0_, this_.collectionId as collect15_0_0_, this_.col_code as col16_0_0_, this_.col_name as col17_0_0_, this_.col_componenttypeid as col18_0_0_, this_.cstateId as cstateId0_0_, this_.cs_code as cs20_0_0_, this_.cs_name as cs21_0_0_, this_.cs_shortcode as cs22_0_0_ from HIBERNATEREQUESTOBJECT2 this_ order by this_.request_id asc ) where rownum <= ?
2008-04-10 19:17:34,901 INFO [net.tp.DateUserType] - < The date coming back Mon Dec 03 00:00:00 EST 2007>
2008-04-10 19:17:34,901 INFO [net.tp.DateUserType] - < The date coming back Mon Dec 03 00:00:00 EST 2007>
2008-04-10 19:17:34,901 INFO [net.tp.DateUserType] - < The date coming back Mon Dec 03 00:00:00 EST 2007>
2008-04-10 19:17:34,916 INFO [net.tp.DateUserType] - < The date coming back Mon Dec 03 00:00:00 EST 2007>
2008-04-10 19:17:34,994 INFO [net.tp.DateUserType] - < The date coming back Mon Dec 03 00:00:00 EST 2007>
2008-04-10 19:17:34,994 INFO [net.tp.DateUserType] - < The date coming back Mon Dec 03 00:00:00 EST 2007>
2008-04-10 19:17:34,994 INFO [net.tp.DateUserType] - < The date coming back Mon Dec 03 00:00:00 EST 2007>
2008-04-10 19:17:34,994 INFO [net.tp.DateUserType] - < The date coming back Mon Dec 03 00:00:00 EST 2007>
2008-04-10 19:17:35,104 INFO [net.tp.DateUserType] - < The date coming back Mon Dec 03 00:00:00 EST 2007>
2008-04-10 19:17:35,104 INFO [net.tp.DateUserType] - < The date coming back Mon Dec 03 00:00:00 EST 2007>
2008-04-10 19:17:35,104 INFO [net.tp.DateUserType] - < The date coming back Mon Dec 03 00:00:00 EST 2007>
2008-04-10 19:17:35,104 INFO [net.tp.DateUserType] - < The date coming back Mon Dec 03 00:00:00 EST 2007>
2008-04-10 19:17:35,197 INFO [net.tp.DateUserType] - < The date coming back Mon Dec 03 00:00:00 EST 2007>
2008-04-10 19:17:35,229 INFO [net.tp.DateUserType] - < The date coming back Mon Dec 03 00:00:00 EST 2007>
2008-04-10 19:17:35,229 INFO [net.tp.DateUserType] - < The date coming back Mon Dec 03 00:00:00 EST 2007>
2008-04-10 19:17:35,229 INFO [net.tp.DateUserType] - < The date coming back Mon Dec 03 00:00:00 EST 2007>
2008-04-10 19:17:35,322 INFO [net.tp.DateUserType] - < The date coming back Mon Dec 03 00:00:00 EST 2007>
2008-04-10 19:17:35,322 INFO [net.tp.DateUserType] - < The date coming back Mon Dec 03 00:00:00 EST 2007>
2008-04-10 19:17:35,322 INFO [net.tp.DateUserType] - < The date coming back Mon Dec 03 00:00:00 EST 2007>
So you can see
<Criteria info CriteriaImpl(net.tp.HibernateRequestObject:this[][dateCreated=Mon Dec 03 00:00:00 EST 2007, ()])>, this is from the actual Criteria.toString() showing the date passed in the query and of course
< The date coming back Mon Dec 03 00:00:00 EST 2007> this is from the UserType implementation which shows the exact same thing, the dates are identical yet it does not match and I receive no results.
Below is my implementation of UserType, modified from my earlier post
Code:
import java.io.Serializable;
import org.hibernate.usertype.UserType;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import java.util.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.apache.log4j.Logger;
/**
* @author kshabazz
*
*This is necessary cause the precision of the returned Timestamp type causes it
*to fail when compared to a Date
*/
public class DateUserType implements UserType
{
private static final int[] SQL_TYPES = new int[] { Types.TIMESTAMP };
private static final Logger log = Logger.getLogger(DateUserType.class);
private static final DateFormat shortDate=new SimpleDateFormat("yyyy-MM-dd");
public DateUserType(){
super();
}
public int[] sqlTypes() {
return SQL_TYPES;
}
public Class returnedClass() {
return java.util.Date.class;
}
public boolean equals(Object x, Object y) throws HibernateException {
if (x instanceof Date) {
Date date1 = (Date) x;
if (y instanceof Date) {
Date date2 = (Date) y;
//if(shortDate.format(date1).equalsIgnoreCase(shortDate.format(date2))){
if(date1.equals(date2)){
return true;
}
}
}
return false;
}
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)throws HibernateException, SQLException
{
Date date;
long milliseconds;
Timestamp timestamp = (Timestamp) Hibernate.TIMESTAMP.nullSafeGet(rs, names[0]);//I think so :(
if( timestamp==null )
{
return null;
}
else
{
milliseconds = timestamp.getTime() + (timestamp.getNanos() / 1000000);
try {
log.info(" The date coming back "+shortDate.parse(shortDate.format(milliseconds)));
return shortDate.parse(shortDate.format(milliseconds));
}
catch(ParseException pe){
throw new RuntimeException(pe.getMessage());
}
}
}
public void nullSafeSet(PreparedStatement st, Object value, int index)throws HibernateException, SQLException
{
// handle the NULL
if( value==null )
{
st.setTimestamp(index, null);
return;
}
if( ! Date.class.isAssignableFrom(value.getClass()) ) {
throw new IllegalArgumentException("Required a (java.util.Date) but received a [" + value.getClass() + "]");
}
Timestamp tstamp = null;
if( (value instanceof Timestamp) )
{
tstamp = (Timestamp) value;
}
else
{
tstamp = new Timestamp( ((Date) value).getTime() );
}
st.setTimestamp(index, tstamp);
}
public Object deepCopy(Object value) throws HibernateException {
if( value==null )
{
return null;
}
else
{
return ((Date) value).clone();
}
}
public boolean isMutable() {
return true;
}
//optional
public Serializable disassemble(Object value) throws HibernateException {
return (Serializable) value;
}
public Object assemble(Serializable cached, Object owner) throws HibernateException {
return cached;
}
public Object replace(Object original, Object target, Object owner) throws HibernateException {
return deepCopy(original);
}
public int hashCode(Object x){
return x.hashCode();
}
}
my criteria factory
Code:
public class CriterionFactoryForFilterTypes {
private final static ArrayList<Criterion> criterion;
private final static SimpleDateFormat format;
private final static Logger log;
static{
criterion = new ArrayList<Criterion>();
format = new SimpleDateFormat("yyyy-MM-dd");
log = Logger.getLogger(CriterionFactoryForFilterTypes.class);
}
public static List<Criterion> matchTypeToCriteria(final List<Filter> filters){
if(!criterion.isEmpty())
criterion.clear();
for(Filter f:filters)
getCriteria(f);
return Collections.unmodifiableList(criterion);
}
private static void getCriteria(final Filter filter){
Date date;
final String type = filter.getType();
if(type.equals("string"))
criterion.add(Restrictions.ilike(filter.getField(),filter.getValue(),MatchMode.ANYWHERE));
if(type.equals("numeric"))
{
switch(filter.getComparison())
{
case eq:criterion.add(Restrictions.eq(filter.getField(), new Long(filter.getValue())));break;
case lt:criterion.add(Restrictions.lt(filter.getField(), new Long(filter.getValue())));break;
case gt:criterion.add(Restrictions.gt(filter.getField(), new Long(filter.getValue())));break;
}
}
if(type.equals("date"))
{
try
{
date = format.parse(filter.getValue().trim());
}
catch(ParseException pe)
{
throw new RuntimeException("The date was not formatted correctly [ format: " + format.toPattern()+" value = "+filter.getValue().trim() + "]");
}
switch(filter.getComparison())
{
case eq:criterion.add(Restrictions.eq(filter.getField(), date));log.info("From the criterion factory "+ date.getTime()+ " and pattern "+format.toPattern());break;
case lt:criterion.add(Restrictions.lt(filter.getField(), date));break;
case gt:criterion.add(Restrictions.gt(filter.getField(), date));break;
}
}
if(type.equals("list"))
{
String[] inParameters = filter.getValue().split(",");
criterion.add(Restrictions.in(filter.getField(),inParameters));
}
}
}
Any ideas anyone???!