我正在为此项目使用Spring boot + jpa + H2。
我需要获取与给定范围重叠的行,因此我建立了查询以检查dateFrom和dateTo字段是否在接收范围内,但是我收到此错误。
我对此错误感到非常困惑,因为我只用Java处理Date Objoects,所以我也将日期存储为Dates,就像存储在数据库中一样。但是在控制台中,它似乎正在尝试将其作为VARCHAR处理。
这是我的模型班:
package com.campsite.reservations.model;
imports
@Entity
@Table(name = "reservations")
public class Reservation {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "reservation_code")
private String reservationCode;
@Column(name = "user_id")
private long userId;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="place_id")
private Place place;
@Column(name = "date_from")
private Date dateFrom;
@Column(name = "date_to")
private Date dateTo;
GETTERS Y SETTER`S
}
这是我的存储库查询:
@Query(
value = "select * FROM Reservations res "+
"where ((res.date_from >= ?1 || res.date_from <= ?2) "+
"|| (res.date_to >= ?1 || res.date_to <= ?2))",
nativeQuery = true)
List<Reservation> findOverlapings(Date dateFrom, Date dateTo);
我使用的是Spring Boot,因此我的控制台的信息看起来有点差。 控制台消息:
2018-11-15 02:44:19.227 DEBUG 9201 --- [nio-8090-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Read [class com.campsite.reservations.model.Reservation] as "application/json;charset=UTF-8" with [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@5b5b53c6]
2018-11-15 02:44:24.271 INFO 9201 --- [nio-8090-exec-1] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select reservatio0_.id as id1_1_, reservatio0_.date_from as date_fro2_1_, reservatio0_.date_to as date_to3_1_, reservatio0_.place_id as place_id6_1_, reservatio0_.reservation_code as reservat4_1_, reservatio0_.user_id as user_id5_1_ from reservations reservatio0_ left outer join places place1_ on reservatio0_.place_id=place1_.id where place1_.id=? and reservatio0_.date_from=? and reservatio0_.date_to=?
Hibernate: select * FROM Reservations res where ((res.date_from >= ? || res.date_from <= ?) || (res.date_to >= ? || res.date_to <= ?))
2018-11-15 02:44:27.595 WARN 9201 --- [nio-8090-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 22007, SQLState: 22007
2018-11-15 02:44:27.598 ERROR 9201 --- [nio-8090-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : Cannot parse "TIMESTAMP" constant "2018-11-01 21:00:00.02018-10-31 21:00:00.0"; SQL statement:
select * FROM Reservations res where ((res.date_from >= ? || res.date_from <= ?) || (res.date_to >= ? || res.date_to <= ?)) [22007-196]
2018-11-15 07:51:26.564 DEBUG 11342 --- [nio-8090-exec-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Written [CampsiteErrorResponse{status=500, message='could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not extract ResultSet', errors=[could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not extract ResultSet]}] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@2b2b7e3c]
答案 0 :(得分:1)
在Reservation
类中查看此内容:
@Column(name = "date_from")
private Date dateFrom;
@Column(name = "date_to")
private Date dateTo;
首先,您没有在实体类中指定@Temporal
注释。更改为:
@Column(name = "date_from")
@Temporal(TemporalType.DATE)
private Date dateFrom;
@Column(name = "date_to")
@Temporal(TemporalType.DATE)
private Date dateTo;
如果您使用spring数据存储库,我建议您利用JpaRepository
并使用如下查询方法:
Collection<Reservation> findByDateFromBetweenOrDateToBetweenOrder(Date dateFrom, Date dateTo);
希望这会有所帮助。