JPA2/Hibernate查询:使用Date作为查询参数,忽略;时间“;部分
我想根据日期字段选择条件查询的一些记录:JPA2/Hibernate查询:使用Date作为查询参数,忽略;时间“;部分,hibernate,date,jpa,criteria,Hibernate,Date,Jpa,Criteria,我想根据日期字段选择条件查询的一些记录: CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(); Root<Macchinario> from = cq.from(Macchinario.class); cq.select(from); cq.where( cb.and(
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery();
Root<Macchinario> from = cq.from(Macchinario.class);
cq.select(from);
cq.where(
cb.and(
cb.like(from.<String>get("marca"), "%"+ricerca.getMarca()+"%"),
cb.like(from.<String>get("modello"), "%"+ricerca.getModello()+"%"),
cb.like(from.<String>get("matricola"), "%"+ricerca.getMatricola()+"%"),
cb.equal(from.get("dataInserimento"), ricerca.getDataInserimento())
)
);
CriteriaBuilder cb=getEntityManager().getCriteriaBuilder();
CriteriaQuery cq=cb.createQuery();
Root-from=cq.from(Macchinario.class);
cq.选择(从中);
哪里(
cb.和(
cb.like(from.get(“marca”),“%”+ricerca.getMarca()+“%”,
cb.like(from.get(“modello”),“%”+ricerca.getModello()+“%”,
cb.like(from.get(“matricola”),“%”+ricerca.getMatricola()+“%”,
cb.equal(来自.get(“dataInserimento”),ricerca.getDataInserimento()
)
);
dataInserimento是一个java.util.Date文件
我要找的“Macchinario”在db中有“2012-05-23 10:16:00”
getDataInserimento()返回“2012-05-23 00:00:00”
我如何传递该参数,告诉jpa忽略日期的“时间部分”
您可以编写一个util并使用它从日期截断时间部分:
DateHelper.java
public static Date getDateWithoutTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
然后改变
cb.equal(from.get("dataInserimento"), ricerca.getDataInserimento())
到
更新
使用JPA或Hibernate提供的开箱即用功能,从db获得的值中截断时间部分似乎是不可能的。Hibernate提供了从date列中提取年、月和日值的功能,我们将使用它
Calendar dateCalendar = Calendar.getInstance();
dateCalendar.setTime(ricerca.getDataInserimento());
Path<Date> dataInserimento = from.get("dataInserimento");
Predicate timelessPredicate = cb.and(
cb.equal(cb.function("year", Integer.class, dataInserimento), dateCalendar.get(Calendar.YEAR)),
cb.equal(cb.function("month", Integer.class, dataInserimento), dateCalendar.get(Calendar.MONTH) + 1),
cb.equal(cb.function("day", Integer.class, dataInserimento), dateCalendar.get(Calendar.DATE)));
cq.where(..., timelessPredicate);
Calendar-dateCalendar=Calendar.getInstance();
dateCalendar.setTime(ricerca.getDataInserimento());
路径dataInserimento=from.get(“dataInserimento”);
谓词timelessPredicate=cb.and(
cb.equal(cb.function(“year”、Integer.class、dataInserimento)、dateCalendar.get(Calendar.year)),
cb.equal(cb.function(“month”、Integer.class、dataInserimento)、dateCalendar.get(Calendar.month)+1),
相等(cb.function(“day”、Integer.class、dataInserimento)、dateCalendar.get(Calendar.DATE));
其中(…,无时间谓词);
我们在这里所做的是,借助hibernate函数和日历功能,将数据库中的年、月和日值与单独提供的输入日期进行比较
这样就可以了。
您可以编写一个util并使用它从日期截断时间部分:
DateHelper.java
public static Date getDateWithoutTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
然后改变
cb.equal(from.get("dataInserimento"), ricerca.getDataInserimento())
到
更新
使用JPA或Hibernate提供的开箱即用功能,从db获得的值中截断时间部分似乎是不可能的。Hibernate提供了从date列中提取年、月和日值的功能,我们将使用它
Calendar dateCalendar = Calendar.getInstance();
dateCalendar.setTime(ricerca.getDataInserimento());
Path<Date> dataInserimento = from.get("dataInserimento");
Predicate timelessPredicate = cb.and(
cb.equal(cb.function("year", Integer.class, dataInserimento), dateCalendar.get(Calendar.YEAR)),
cb.equal(cb.function("month", Integer.class, dataInserimento), dateCalendar.get(Calendar.MONTH) + 1),
cb.equal(cb.function("day", Integer.class, dataInserimento), dateCalendar.get(Calendar.DATE)));
cq.where(..., timelessPredicate);
Calendar-dateCalendar=Calendar.getInstance();
dateCalendar.setTime(ricerca.getDataInserimento());
路径dataInserimento=from.get(“dataInserimento”);
谓词timelessPredicate=cb.and(
cb.equal(cb.function(“year”、Integer.class、dataInserimento)、dateCalendar.get(Calendar.year)),
cb.equal(cb.function(“month”、Integer.class、dataInserimento)、dateCalendar.get(Calendar.month)+1),
相等(cb.function(“day”、Integer.class、dataInserimento)、dateCalendar.get(Calendar.DATE));
其中(…,无时间谓词);
我们在这里所做的是,借助hibernate函数和日历功能,将数据库中的年、月和日值与单独提供的输入日期进行比较
这样就可以了。您可以尝试使用适当的模式格式化日期以忽略时间部分
public String getDate(java.util.Date date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
现在,您可以将日期作为字符串以适当的格式显示&然后比较它们是否相等
cb.equal(getDate(from.<java.util.Date>get("dataInserimento")), getDate(ricerca.getDataInserimento()))
cb.equal(getDate(from.get(“dataInserimento”)),getDate(ricerca.getDataInserimento())
您可以尝试使用适当的模式格式化日期以忽略时间部分
public String getDate(java.util.Date date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
现在,您可以将日期作为字符串以适当的格式显示&然后比较它们是否相等
cb.equal(getDate(from.<java.util.Date>get("dataInserimento")), getDate(ricerca.getDataInserimento()))
cb.equal(getDate(from.get(“dataInserimento”)),getDate(ricerca.getDataInserimento())
uhmm。。。。。我想你不明白。My ricerca.getDataInserimento()已返回一个日期时间,其中(00:00:00)作为时间部分。数据库中的记录仍有“10:16:00”。那么,在比较不同日期时,比较如何返回“找到”结果呢?我需要做一个比较,避免比较时间部分。这是一个技巧,但是。。。。。非常棘手:)我不敢相信所有这些都适用于这样一个常见的查询。如果使用JPQL替代查询,您的答案将是完美的;)谢谢你来考虑一下。。您可以用where date>=selectedDate和date