简体   繁体   English

Hibernate查询未检索结果?

[英]Hibernate query is not retriving the result?

Here is bean class, 这是豆类,

@Entity
@Table(name="hlatlng")
public class HistoryLatitudeBean {
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@Column(name="vehicleno")
private int vehicleno;
@Column(name="lat")
private String lat;
@Column(name="lng")
private String lng;
@Column(name="status")
private String status;
@Column(name="rdate")
private Date rdate;
@Column(name="rtime")
private Date rtime;
//getter setter,
}

In Hibernate method I am writing, 我正在用Hibernate方法编写,

          Map<String, Object> parameterNameAndValues = new HashMap<String, Object>();

          parameterNameAndValues.put("vehicleno", 12);
          parameterNameAndValues.put("frmdate", frmDate);
          parameterNameAndValues.put("todate", toDate);

          hql= "from HistoryLatitudeBean where vehicleno=:vehicleno and rdate BETWEEN :frmdate and :todate";
          Query query =session.createQuery(hql);
            /*query.setParameter("vehicleno", 12);
            query.setParameter("frmdate", frmDate);
            query.setParameter("todate", toDate);*/


          for (Entry<String, Object> e : parameterNameAndValues.entrySet()) {
                query.setParameter(e.getKey(), e.getValue());
            }

            List<HistoryLatitudeBean> groupList = (List<HistoryLatitudeBean>)query.list();

            //Here groupList contains null              

             for(HistoryLatitudeBean arr : groupList){
                 vehicleHistoryList.add(arr);    
                 System.out.println("List :"+arr.getLat());
                }


             transaction.commit();

Problem is query.list() method returns null. 问题是query.list()方法返回null。 The same query I am trying in mysql db as, SELECT * FROM hlatlng WHERE vehicleno='12' AND rdate BETWEEN '2014-01-01' AND '2014-09-01'; 我正在mysql数据库中尝试的相同查询,例如SELECT * FROM hlatlng WHERE vehicleno='12' AND rdate BETWEEN '2014-01-01' AND '2014-09-01';

and my table structure is like this, 我的表结构是这样的,

CREATE TABLE `hlatlng` (
 `vehicleno` int(40) DEFAULT NULL,
 `lat` varchar(40) DEFAULT NULL,
 `lng` varchar(40) DEFAULT NULL,
 `status` varchar(40) DEFAULT NULL,
 `rdate` date DEFAULT NULL,
 `rtime` date DEFAULT NULL,
 `id` int(40) NOT NULL AUTO_INCREMENT,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

It gives me 3 rows. 它给了我3行。 I am not getting what is wrong in my code , can any one help me in this please. 我没有得到我代码中的错误,请任何人可以帮助我。

The Date in java will have the time part associated with it. java中的Date将具有与之关联的时间部分。 If our data is only on dates, we need to trim the time part. 如果我们的数据仅是日期,则需要修剪时间部分。

Adding the @Type(type="date") will take care of this while sending it to the database. 添加@Type(type="date")会在将其发送到数据库时解决此问题。

I tried to find a reference, unfortunately I could not find anything. 我试图找到一个参考,很遗憾,我找不到任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM