[英]Type mismatch: number, date or string expected hibernate LocalDate
Task:任务:
The field dates
has type date
in the database .字段dates
在数据库中具有类型date
。
create table items
(
id serial primary key,
dates date
);
Entity : // field "date", type "LocalDate":实体://字段“日期”,输入“LocalDate”:
@Entity
@Table(name = "items")
public class Item {
private LocalDate date;
@Column(name = "dates")
public LocalDate getDate() {
return date;
}
}
class UserStore: class 用户存储:
class UserStore {
private final SessionFactory factory = new Configuration()
.configure().buildSessionFactory();
public List<?> findByDate() {
Session session = factory.openSession();
session.beginTransaction();
final String sql = "from Item i where i.date between ?1 and ?2";
Query query = session.createQuery(sql, Item.class);
query.setParameter(1, LocalDate.now());
query.setParameter(2, LocalDate.now());
List<?> result = query.getResultList();
session.getTransaction().commit();
session.close();
return result;
}
}
pom.xml pom.xml
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.12.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-java8 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-java8</artifactId>
<version>5.4.17.Final</version>
</dependency>
added:添加:
<property name="dialect">org.hibernate.dialect.PostgreSQL9Dialect</property>
in the sql request: "from Item i where i.date between?1 and?2", field i.date is highlighted in red, indicating a jpa error and requiring the use of the Date type:在 sql 请求中:“来自 Item i where i.date between?1 and?2”,字段i.date以红色突出显示,表示jpa 错误并需要使用 Date 类型:
If i use type Date
instead LocalDate
mistake escape.如果我使用类型Date
而不是LocalDate
错误转义。 But I need LocalDate
type.但我需要LocalDate
类型。
How to fix it?如何解决?
ps.附言。 The code compiles correctly.code implementation is not important.代码编译正确。代码实现并不重要。 question one: How to remove the error without turning off the inspection.问题一:如何在不关闭检查的情况下排除错误。
Remove the hibernate-java8
artifact from your pom.xml
.从pom.xml
中删除hibernate-java8
工件。 It is deprecated .它已被弃用。 The hibernate-core
will be enough. hibernate-core
就足够了。 I have tested your query and it works fine for me with LocalDate
.我已经测试了您的查询,它对我来说很好用LocalDate
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.