-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Date restriction in SQLServer fails if field contains time.
PostPosted: Mon Oct 09, 2006 1:41 am 
Beginner
Beginner

Joined: Wed Aug 25, 2004 10:27 pm
Posts: 21
Location: Indonesia
Hi all,

Hibernate version: 3.0
Name and version of the database you are using: SQL Server 2000

I created the following mapping :
Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.farbeyond.server.dao.entities.BranchImpl"
              table="Branches">

        <id name="rowId" column="rowId" type="int">
            <generator class="native">
            </generator>
        </id>

        <property name="rowId"
                        type="int"
                        update="false"
                        insert="false"
                        column="rowId"/>

        <property name="branchId"
                        type="int" update="true"
                        insert="true"
                        column="branchId"/>

        <property name="branchName"
                        type="java.lang.String"
                        update="true"
                        insert="true"
                        column="branchName"/>

        <property name="dateTimeCreated"
                        type="date"
                        update="true"
                        insert="true"
                        column="dateTimeCreated"/>
   </class>
</hibernate-mapping>


I insert record using the following query :
Code:
INSERT INTO Branches (branchId,branchName,dateTimeCreated)
VALUES (1,'Branch 1','2005-01-03 14:12:41')


Then I execute Hibernate Criteria :
Code:
...
Calendar cal = Calendar.getInstance();
cal.set(2005,Calendar.JANUARY,3);

criteria.add(Restrictions.eq("dateTimeCreated",cal.getTime()));
List list = getHibernateTemplate().findByCriteria(criteria,0,100)

System.out.println("size : " + list.size());  //returns no record.
...

The above criteria returns no record.

I also tried to change the calendar to:
Code:
cal.set(2005,Calendar.JANUARY,3,14,12,41);

the result is the same, no record.

But if I insert record using the following query :
Code:
INSERT INTO Branches (branchId,branchName,dateTimeCreated)
VALUES (1,'Branch 1','2005-01-03')


and execute the same Hibernate Criteria using calendar with or without the time parameters, it returns the desired records.

I run the same test under PostgreSQL without problems. I guess this is due to the SQL Server date data type that combines date and time at once instead of separate date and time data type as provided by PostgreSQL.

I have tables that might be queried against its datetime field with or without its time, so I need to find one-fits-all solution on this problem. Worse thing is I also need the solution to be portable accross SQLServer, PostgreSQL and MySQL databases.

Any help would be greatly appreciated.


Best Regards,


Setya


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 09, 2006 3:40 pm 
Beginner
Beginner

Joined: Tue Sep 26, 2006 11:46 pm
Posts: 33
Just a guess, but try setting the milliseconds of the Calendar to 0.

If memory serves they're not automatically zeroed.

You will definitely need to include the time in your calendar as

03/01/2005 is not equal to 03/01/2005 14:12:41


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 31, 2006 12:40 pm 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
Setya,

This looks like a timezone issue. Try setting the timezone to the timezone of the server.

Marius


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.