类型不匹配:数字、日期或字符串预期休眠 LocalDate

mai*_*ime 5 java hibernate jpa mismatch

任务:


该字段在数据库dates中具有类型。date

create table items
(
    id   serial primary key,
    dates date
);
Run Code Online (Sandbox Code Playgroud)

Entity : // 字段“date”,输入“LocalDate”:

@Entity
@Table(name = "items")
public class Item {
    private LocalDate date;

    @Column(name = "dates")
    public LocalDate getDate() {
        return date;
    }
}
Run Code Online (Sandbox Code Playgroud)

用户存储类:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

添加:

 <property name="dialect">org.hibernate.dialect.PostgreSQL9Dialect</property>
Run Code Online (Sandbox Code Playgroud)

在sql请求中:“from Item i where i.date between ?1 and ?2”,字段i.date以红色突出显示,表示jpa错误,需要使用Date类型:

看看错误

如果我使用 typeDate而不是LocalDate错误转义。但我需要LocalDate打字。


如何修复它?

附:代码编译正确。代码实现并不重要。问题一:如何在不关闭检查的情况下排除错误。