在JPA实体类中用于定义列的数据类型是否会影响DAO中查询的创建?

huangapple go评论81阅读模式
英文:

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

问题

在Java的实体类中,我为列定义了一个命名查询和数据类型。我在DAO类中执行了这个查询,但是遇到了错误:java.lang.IllegalArgumentException: Type specified for TypedQuery [TestEntity] is incompatible with query return type [class java.util.Date]。在定义mydate列时,我没有使用java.util.Date类,而是使用了java.sql.Timestamp,所以不清楚为什么错误提到了java.util.Date。看起来我做错了什么,但是不清楚具体是什么问题。

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

public class TestEntity {

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

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

在DAO类中:

public class TestDao 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();
        });
    }
}
英文:

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.

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

public class TestEntity {


    @Id
    @Column(name = &quot;MY_ID&quot;)
    private String my_id;

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

In DAO class

public classTest Dao extends BaseDao

  public List&lt;TestEntity&gt; findByTestId(String id) {
            return execute(&quot;punit&quot;, entityManager -&gt; {
                TypedQuery&lt;TestEntity&gt; query = entityManager.createNamedQuery(&quot;findByTestId&quot;, TestEntity.class);
                query.setParameter(&quot;testId&quot;, id);
                return query.getResultList();
            });
        }

答案1

得分: 1

请尝试使用以下代码,它将起作用。

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

查询返回类型需要与您想要转换的实体对象兼容。更新您的代码:

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

此方法的返回类型应该类似于 List<Date>
这将有助于您。

英文:

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

   @Column(name = &quot;mydate&quot;)
    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&lt;Date&gt;date= entityManager.createNamedQuery(&quot;findByTestId&quot;, Date.class);

And return type of this method should be like List&lt;Date&gt;.
This will help you.

答案2

得分: 1

根据您的代码,您应该将查询更改为以下内容:

@NamedQuery(name = "findByTestId", query = "select u from TestEntity u where u.my_id = :testId")

如果您只想获取日期,您需要编辑您的方法。

英文:

Based on your code. you should change the query to this

@NamedQuery(name = &quot;findByTestId&quot;, query = &quot;select u from TestEntity u where u.my_id = :testId&quot;)

if you want to get only the date you have to edit your method.

答案3

得分: 0

你的查询应该返回一个 java.util.Timestamp 对象列表,因为你在选择列表中只包含了 "mydate" 字段,但是在 findByTestId 方法中创建的类型查询中,你指示 JPA 返回一个 TestEntity 对象列表。最简单的修复方法是修改你的查询如下:

"select u from TestEntity u where u.my_id = :testId"

此外,java.sql.Timestampjava.util.Date 的子类,这可能解释了为什么错误指向了 java.util.Date

英文:

Your query is supposed to return a list of java.util.Timestamp objects since you have included only "mydate" field in the select list, but in the creation of the typed query in findByTestId method you are instructing JPA to return a List of TestEntity objects. The easiest way to fix this is to alter your query like this:

&quot;select u from TestEntity u where u.my_id = :testId&quot;

In addition, java.sql.Timestamp is a subclass of java.util.Date, which may explain why the error refers to java.util.Date.

huangapple
  • 本文由 发表于 2020年8月20日 19:37:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63504234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定