【发布时间】:2014-03-05 07:04:26
【问题描述】:
在使用 hibernate 和 jpa 的 spring mvc 应用程序中,我有一个查询需要返回日期在指定日期内的所有记录。 day 最终来自yyyy-mm-dd 格式的url 参数,并存储在joda localdate 对象中。按天搜索其记录的实体将 joda DateTime 作为正在搜索的属性的类型。底层 MySQL 数据库将日期信息存储在 TimeStamp 字段中。为了从localdate 转换为datetime,我阅读了the localdate api 并尝试了day.toDateTimeAtStartOfDay(),但这会将参数范围缩小太多。我需要在一天中的 24 小时内获取所有记录,而不仅仅是一天中第一秒的记录。如何构造查询,使其返回 TimeStamp 字段中指定日期的每条记录,包括该指定日期内的所有时间?
这里是在 jpa 存储库中包含查询的方法:
@Override
public Collection<Encounter> findByDay(LocalDate day){
System.out.println("@@@@@@@@@@@@@@@ made it into findByDay()");
DateTime myDT = day.toDateTimeAtStartOfDay();
Query query = this.em.createQuery("SELECT DISTINCT encounter FROM Encounter encounter WHERE encounter.dateTime LIKE :day");
query.setParameter("day", myDT);
return query.getResultList();
}
这是 MySQL 中底层数据表的 sql:
CREATE TABLE IF NOT EXISTS encounters(
id int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
patient_id int(11) UNSIGNED NOT NULL,
office_id int(11) UNSIGNED NOT NULL,
provider_id int(11) UNSIGNED NOT NULL,
start_date TIMESTAMP,
status varchar(50),
FOREIGN KEY (patient_id) REFERENCES patients(id),
FOREIGN KEY (office_id) REFERENCES facilityAddresses(id),
FOREIGN KEY (provider_id) REFERENCES providers(id)
)engine=InnoDB;
这里是Encounter实体的相关部分:
@Entity
@Table(name = "encounters")
public class Encounter extends BaseEntity{
@Column(name="start_date")
private DateTime dateTime;
//other mappings of columns to properties
// getters and setters
}
编辑:
这是我根据 Affe 的建议测试的内容:
@Override
public Collection<Encounter> findByDay(LocalDate day){
System.out.println("kkkkkkkkkkkkkkkkkkkkkk inside repository.findByDay() ");
DateTime startDay = day.toDateTimeAtStartOfDay();
DateTime startNextDay = startDay.plusDays(1);
System.out.println("startDay is: "+startDay);
System.out.println("startNextDay is: "+startNextDay);
Query query = this.em.createQuery(
"SELECT DISTINCT encounter " +
"FROM Encounter encounter " +
"WHERE encounter.dateTime >= :startDay and encounter.dateTime < :startNextDay");
query.setParameter("startDay", startDay);
query.setParameter("startNextDay", startNextDay);
Collection<Encounter> myresult = query.getResultList();
Object[] something = myresult.toArray();
System.out.println("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv num results is: "+myresult.size());
for(int i=0;i<something.length;i++){
System.out.println("nnnnnnnnnnnnnnnnnnnnnnnnn "+something[i].toString());
}
return myresult;
}
这是将测试数据插入数据库的sql:
INSERT IGNORE INTO encounters VALUES (1, 5, 2, 1, '2013-11-30 09:00:00','Active');
INSERT IGNORE INTO encounters VALUES (2, 4, 1, 2, '2013-12-15 10:00:00','Inactive');
INSERT IGNORE INTO encounters VALUES (3, 3, 4, 3, '2014-1-10 11:00:00','Active');
INSERT IGNORE INTO encounters VALUES (4, 2, 3, 4, '2014-1-13 12:00:00','Inactive');
INSERT IGNORE INTO encounters VALUES (5, 1, 1, 3, '2014-1-12 13:00:00','Inactive');
当我选择2014-1-10、2014-1-13、2014-1-12 或其他插入的日期作为参数时,上面的system.out.println() 打印出结果数为零,因此 for 循环永远不会运行。查询应该为每一天返回一个结果。
例如,当我选择2014-1-10时,eclipse控制台输出如下:
kkkkkkkkkkkkkkkkkkkkkk inside repository.findByDay()
startDay is: 2014-01-10T00:00:00.000-08:00
startNextDay is: 2014-01-11T00:00:00.000-08:00
Hibernate: select distinct encounter0_.id as id1_10_, encounter0_.start_date as start2_10_, encounter0_.office_id as office4_10_, encounter0_.patient_id as patient5_10_, encounter0_.status as status3_10_ from encounters encounter0_ where encounter0_.start_date>=? and encounter0_.start_date<?
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv num results is: 0
number of encounters is: 0
aaaa hours of day, mins of hour for block: 8, 0
aaaa hours of day, mins of hour for block: 9, 0
aaaa hours of day, mins of hour for block: 10, 0
aaaa hours of day, mins of hour for block: 11, 0
aaaa hours of day, mins of hour for block: 12, 0
aaaa hours of day, mins of hour for block: 13, 0
aaaa hours of day, mins of hour for block: 14, 0
aaaa hours of day, mins of hour for block: 15, 0
aaaa hours of day, mins of hour for block: 16, 0
System.out.println()输出的datetime的格式与问题有关系吗?我做错了什么?
【问题讨论】:
标签: java spring hibernate spring-mvc jpa