英文:
findByDate in spring boot jpa gives empty list
问题
以下是翻译好的内容:
我正在使用Spring Boot JPA进行CRUD操作。我正在使用字段名为created_at的数据库表进行查询,该字段的类型为date。表中有一些具有给定日期的行,但是JPA返回了零个结果集。我正在使用Oracle 11g。
这是我的实体类:
import java.sql.Date;
@Entity
@Table(name="veev_json")
public class VeevJson {
@Id
@Column(name="ID")
private int id;
@Column(name="CREATED_AT")
private Date createdDate;
}
我的JPA Repository:
import java.util.Date;
@Repository
public interface VeevJsonRepository extends JpaRepository<VeevJson, Integer> {
public List<VeevJson> findByCreatedDate(Date date);
}
在服务层调用该函数:
Date date = new Date(); //获取当前java.util.Date类型的日期
List<VeevJson> documents = veevJsonRepository.findByCreatedDate(date);
我的数据库表结构:
ID NUMBER(10,0)
CREATED_AT DATE
Hibernate生成的SQL查询:
select veevjson0_.ID as ID1_1_, veevjson0_.CREATED_AT as CREATED_AT2_1_, veevjson0_.JSON as JSON3_1_, veevjson0_.STATUS as STATUS4_1_ from veev_json veevjson0_ where veevjson0_.CREATED_AT=?
英文:
I'm using spring boot JPA for CRUD operations. I'm querying the database table with the field name created_at which is of type date.There are some rows in the table with the given date but JPA is giving zero result set. I'm using Oracle 11g
Here is my entity
import java.sql.Date;
@Entity
@Table(name="veev_json")
public class VeevJson {
@Id
@Column(name="ID")
private int id;
@Column(name="CREATED_AT")
private Date createdDate;
}
My JPA Repository
import java.util.Date;
@Repository
public interface VeevJsonRepository extends JpaRepository<VeevJson, Integer> {
public List<VeevJson> findByCreatedDate(Date date);
}
Calling the function in the service layer
Date date = new Date(); //taking current date of type java.util.Date
List<VeevJson> documents = veevJsonRepository.findByCreatedDate(date);
My DB table structure
ID NUMBER(10,0)
CREATED_AT DATE
SQL query generated by the hibernate:
select veevjson0_.ID as ID1_1_, veevjson0_.CREATED_AT as CREATED_AT2_1_, veevjson0_.JSON as JSON3_1_, veevjson0_.STATUS as STATUS4_1_ from veev_json veevjson0_ where veevjson0_.CREATED_AT=?
答案1
得分: 1
使用类型为Date的字段时,还应使用@Temporal注解。@Temporal的默认值为TemporalType.TIMESTAMP,而您的JPA实现可能会在处理类型为java.util.Date的字段时感到困惑,将时间戳而不是日期作为查询参数传递。
请将您的字段注释如下:
import java.util.Date;
@Entity
@Table(name = "veev_json")
public class VeevJson {
@Id
@Column(name = "ID")
private int id;
@Temporal(TemporalType.DATE)
@Column(name = "CREATED_AT")
public Date createdDate;
...
}
这样做将使JPA实现只发送日期(可能以'YYYY-MM-dd'格式)作为查询值,而不是时间戳或任何其他值。
如果您愿意,并且您的JDBC支持4.1版本,您可以将java.util.Date替换为java8时间API类型,但我想这在这里不在讨论范围内。
英文:
When using a field with type Date, you should also use the @Temporal annotation. The default value of @Temporal is TemporalType.TIMESTAMP, and your JPA implementation may get confused about dealing with a field of type java.util.Date, passing as argument of query the timestamp instead of date.
Please annotate your field as
import java.util.Date;
@Entity
@Table(name = "veev_json")
public class VeevJson {
@Id
@Column(name = "ID")
private int id;
@Temporal(TemporalType.DATE)
@Column(name = "CREATED_AT")
public Date createdDate;
...
}
Doing so will allow JPA implementation to send as queried value only the date (probably in 'YYYY-MM-dd' format) instead of timestamp or any other value.
If you prefer and your JDBC supports 4.1 version, you may exchange the java.util.Date for java8 time API types, but I guess this is out of scope here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论