-->
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: problem with dates on Criteria
PostPosted: Tue Oct 16, 2007 12:00 pm 
Newbie

Joined: Fri Jul 20, 2007 7:04 am
Posts: 6
Hibernate version: 3

Name and version of the database you are using: MySQL 5


Code:
Criteria crit;
                    Criteria critCount;
                   
                    String col = conditions.getCondition(0).getColumn();
                   
                    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                    Date date=df.parse(conditions.getCondition(0).getValue());
                   
                    crit = session.createCriteria(Person.class);
                    critCount = session.createCriteria(Person.class);
                    critCount.setProjection( Projections.rowCount() );
                   
                    Criterion c = Restrictions.eq(col,date);

                    crit.add(c);
                    critCount.add(c);

                    /* Defines the ordering condition */
                    if(orderAsc) crit.addOrder(Property.forName(orderCol).asc());
                    else crit.addOrder(Property.forName(orderCol).desc());

                    crit.setMaxResults(pageSize);
                    crit.setFirstResult(firstRow);

                    Object total = critCount.uniqueResult();
                    List list = crit.list();



Well my purpose with this code, is to get a String converted it to a Date, according the format, and make a criteria based on that.

But i get no error output and it return 0 rows. Have I implemented something wrong on this code?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 12:28 pm 
Regular
Regular

Joined: Mon Aug 07, 2006 6:22 pm
Posts: 67
It might help to turn on sql logging, setting the following properties (in decreasing importance):

* hibernate.show_sql
* hibernate.format_sql
* hibernate.use_sql_comments

Set them all to "true".

Also, for comparing dates, I believe you need to look carefully at the actual value stored in the column. It might not be useful to try to compare a date value directly. Relative compares would probably work, but checking for "equal" might be problematic.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 4:26 pm 
Beginner
Beginner

Joined: Fri Jun 25, 2004 11:47 am
Posts: 34
Hi,

Little question, what do you have for a value in the database?
i.e. [10.10.2007 11:45:23] or [10.10.2007 00:00:00]
Check if the time is set in db. The formatting above of the date left the time empty.

rgds


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.