5

Task:


The field dates has type date in the database.

create table items
(
    id   serial primary key,
    dates date
);

Entity: // field "date" , type "LocalDate":

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

    @Column(name = "dates")
    public LocalDate getDate() {
        return date;
    }
}

class UserStore:

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

        <!-- 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:

to look a mistake

If i use type Date instead LocalDate mistake escape. But I need LocalDate type.


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.

SternK
  • 11,649
  • 22
  • 32
  • 46
mailtime
  • 111
  • 1
  • 8
  • What hibernate dialect do you use? – SternK May 31 '20 at 15:59
  • @SternK Hi. .PostgreSQL9Dialect – mailtime May 31 '20 at 16:15
  • Hi Mailtime, concerning something you asked elsehwere: Do you know that you can sort the view of answers to a question by age, votes and activity? It is the reason why for example "above answer" is usually an ambiguous reference. – Yunnosch Aug 25 '20 at 13:54

1 Answers1

0

Remove the hibernate-java8 artifact from your pom.xml. It is deprecated. The hibernate-core will be enough. I have tested your query and it works fine for me with LocalDate.

SternK
  • 11,649
  • 22
  • 32
  • 46