Does the data type used in JPA entity class for defining a column impact the creation of a query in DAO?

Tags: , ,

In java entity class, I defined a named query and data types for columns. I executed the query in a DAO class but got the error: java.lang.IllegalArgumentException: Type specified for TypedQuery [TestEntity] is incompatible with query return type [class java.util.Date]. I have not used a java.util.Date class for defining the mydate column, instead I used java.sql.Timestamp, so it’s unclear why the error is referring to java.util.Date. It looks like I have done something wrong, but it’s unclear what it is.

@Table(name = "SAMPLE", schema = "MYSCHEMA")
@NamedQuery(name = "findByTestId", query = "select u.mydate from TestEntity u where u.my_id = :testId")

public class TestEntity {

    @Column(name = "MY_ID")
    private String my_id;

    @Column(name = "mydate")
    private java.sql.Timestamp mydate;

In DAO class

public classTest Dao extends BaseDao

  public List<TestEntity> findByTestId(String id) {
            return execute("punit", entityManager -> {
                TypedQuery<TestEntity> query = entityManager.createNamedQuery("findByTestId", TestEntity.class);
                query.setParameter("testId", id);
                return query.getResultList();


Could you please try with below code, it will work.

   @Column(name = "mydate")
    private Date mydate;

The query return type need be compatible with the entity object in which you want to cast it. Update your code

TypedQuery<Date>date= entityManager.createNamedQuery("findByTestId", Date.class);

And return type of this method should be like List<Date>. This will help you.

Source: stackoverflow