-->
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.  [ 6 posts ] 
Author Message
 Post subject: Hibernate can not query the right resule while native sql c
PostPosted: Tue Feb 22, 2011 4:40 am 
Beginner
Beginner

Joined: Fri Nov 05, 2010 7:25 am
Posts: 21
This is the test code,first use hibernate ,then use the native sql:


Code:
   @Test
   public void testSQLAndHibernate() throws ParseException {
      start = sdf.parse("2011-02-22 10:00:00");
      end = sdf.parse("2011-02-22 16:00:00");
      // use hibernate
      Session sess = HibernateUtil.getSessionFactory().getCurrentSession();
      sess.beginTransaction();

      Query q = sess
            .createQuery(
                  "select log.uri,count(log.uri) as num from LogEntry log where log.time between ? and ? group by log.uri order by num desc")
            .setDate(0, start).setDate(1, end);
      System.out.println("get "+q.list().size()+" results by hibernate");

      System.out.println("++++++++++++++");
      // use sql
      SimpleDateFormat sdf_sql = new SimpleDateFormat("yyyyMMddHHmmss");
      String sql = "select uri,count(uri) as num from t_log where time between "
            + sdf_sql.format(start) + " and " + sdf_sql.format(end)
            + " group by uri order by num desc";
      try {
         Class.forName("com.mysql.jdbc.Driver");
         Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db",
               "root", "0000");
         Statement state = conn.createStatement();
         ResultSet rs = state.executeQuery(sql);
         int i=0;
         while (rs.next()) {
            //System.out.println(rs.getString(1) + " " + rs.getLong(2));
            i++;
         }
         System.out.println("get "+i+" results by sql query");
      } catch (ClassNotFoundException e) {
         e.printStackTrace();
      } catch (SQLException e) {
         e.printStackTrace();
      }
   }



OutPut:

get 0 results by hibernate
++++++++++++++
get 24 results by sql query

Also from the hibernat log I get :

Code:
binding parameter [1] as [DATE] - Tue Feb 22 10:00:00 CST 2011
binding parameter [2] as [DATE] - Tue Feb 22 16:00:00 CST 2011


It seems that the start and end date are correct, so I wonder if the format is right or not?


Top
 Profile  
 
 Post subject: Re: Hibernate can not query the right resule while native sql c
PostPosted: Tue Feb 22, 2011 6:48 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
It must be a problem with the date format.
You should activate the show.sql property in your hibernate config to see the SQL query generated by hibernate ...


Top
 Profile  
 
 Post subject: Re: Hibernate can not query the right resule while native sql c
PostPosted: Tue Feb 22, 2011 8:50 am 
Beginner
Beginner

Joined: Fri Nov 05, 2010 7:25 am
Posts: 21
overmeulen wrote:
It must be a problem with the date format.
You should activate the show.sql property in your hibernate config to see the SQL query generated by hibernate ...

I have activate the show sql propety.

And in the sql generated by hibernate,the start and end date are all "?",so I can only see them according to the log-- the parameter binding.


Top
 Profile  
 
 Post subject: Re: Hibernate can not query the right resule while native sql c
PostPosted: Tue Feb 22, 2011 9:22 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Try using setTimestamp() instead of setDate() when passing parameters, if I remember well setDate() only keeps the day, month and year of the date


Top
 Profile  
 
 Post subject: Re: Hibernate can not query the right resule while native sql c
PostPosted: Tue Feb 22, 2011 9:54 am 
Beginner
Beginner

Joined: Fri Nov 05, 2010 7:25 am
Posts: 21
overmeulen wrote:
Try using setTimestamp() instead of setDate() when passing parameters, if I remember well setDate() only keeps the day, month and year of the date

Thanks, I will have a try.


Top
 Profile  
 
 Post subject: Re: Hibernate can not query the right resule while native sql c
PostPosted: Wed Feb 23, 2011 2:32 am 
Beginner
Beginner

Joined: Fri Nov 05, 2010 7:25 am
Posts: 21
apachemaven wrote:
overmeulen wrote:
Try using setTimestamp() instead of setDate() when passing parameters, if I remember well setDate() only keeps the day, month and year of the date

Thanks, I will have a try.

It works now,thank you very much.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.