英文:
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 = "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;
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();
});
}
答案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 = "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.
答案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 = "findByTestId", query = "select u from TestEntity u where u.my_id = :testId")
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.Timestamp
是 java.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:
"select u from TestEntity u where u.my_id = :testId"
In addition, java.sql.Timestamp is a subclass of java.util.Date, which may explain why the error refers to java.util.Date.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论