NamedQuery 返回具有空字段的实体
NamedQuery returning entities with null fields
使用 namedquery 定义的实体为 SELECT mdl FROM tbl_slots mdl where mdl.test_date between :dt and :dt order by mdl.test_time asc
如果在查询中使用 *
而不是 mdl
,JPA 会给出错误 unexpected token: *
如果在 select 语句中提到列名,则 returns 实体的各个字段填充了预期值
[{
"srNo": 1,
"testDate": "2021-Dec-30",
"testTime": "09:00-10:00",
},{
"srNo": 2,
"testDate": "2021-Dec-30",
"testTime": "11:00-12:00",
}]
如何在不在 select 语句中提及列名的情况下获得与使用 *
? 相同的结果
select * from table
查询的有效 JPQL 或 HQL 是什么?
实体class
@Table(name = "tbl_slots")
@NamedQueries({
@NamedQuery(name="slots",query = "SELECT mdl FROM tbl_slots mdl where mdl.test_date between :dt and :dt order by mdl.test_time asc")
})
public class TblSlots implements Serializable {
private Long srNo;
private Date testDt;
private String testTime;
public TblSlots() {}
public TblSlots(Long srNo, Date testDt, String testTime) {
this.srNo = srNo;
this.testDt = testDt;
this.testTime = testTime;
}
@Id
@Column(name = "sr_no", unique = true, nullable = false, precision = 16, scale = 0)
public Long getSrNo() {
return this.srNo;
}
public void setSrNo(Long srNo) {
this.srNo = srNo;
}
@Temporal(TemporalType.DATE)
@Column(name = "test_date", nullable = false, length = 13)
public Date getTestDt() {
return this.testDt;
}
public void setTestDt(Date testDt) {
this.testDt = testDt;
}
@Column(name = "test_time", nullable = false, length = 20)
public String getTestTime() {
return this.testTime;
}
public void setTestTime(String testTime) {
this.testTime = testTime;
}
你不能使用'*',因为Hibernate 正在转动对象。
如果要使用 *,请使用 @NamedNativeQuery
而不是 @NamedQuery
@NamedQueries({
@NamedQuery(name="slots",query = "SELECT mdl FROM Member mdl ")
})
// Use this
@NamedNativeQueries({
@NamedNativeQuery(name="slots_native",query = "SELECT * FROM Member mdl ",resultClass = MemberEntity.class)
})
JPQL 理解 java 个实体而不是列。
how to get same result without mentioning column names in select
statement as if used *?
what's valid JPQL or HQL for select * from table query?
所以一个有效的 JQPL SELECT 查询甚至可以没有 SELECT
子句。
以下是对 return 来自 table
的完整实体的有效 JPQL 查询
FROM tbl_slots mdl where mdl.test_date between :dt and :dt order by mdl.test_time asc
所以你的注释可以写成
@NamedQueries({
@NamedQuery(name="slots",query = "FROM tbl_slots mdl where mdl.test_date between :dt and :dt order by mdl.test_time asc")
})
使用 namedquery 定义的实体为 SELECT mdl FROM tbl_slots mdl where mdl.test_date between :dt and :dt order by mdl.test_time asc
如果在查询中使用 *
而不是 mdl
,JPA 会给出错误 unexpected token: *
如果在 select 语句中提到列名,则 returns 实体的各个字段填充了预期值
[{ "srNo": 1, "testDate": "2021-Dec-30", "testTime": "09:00-10:00", },{ "srNo": 2, "testDate": "2021-Dec-30", "testTime": "11:00-12:00", }]
如何在不在 select 语句中提及列名的情况下获得与使用 *
? 相同的结果
select * from table
查询的有效 JPQL 或 HQL 是什么?
实体class
@Table(name = "tbl_slots")
@NamedQueries({
@NamedQuery(name="slots",query = "SELECT mdl FROM tbl_slots mdl where mdl.test_date between :dt and :dt order by mdl.test_time asc")
})
public class TblSlots implements Serializable {
private Long srNo;
private Date testDt;
private String testTime;
public TblSlots() {}
public TblSlots(Long srNo, Date testDt, String testTime) {
this.srNo = srNo;
this.testDt = testDt;
this.testTime = testTime;
}
@Id
@Column(name = "sr_no", unique = true, nullable = false, precision = 16, scale = 0)
public Long getSrNo() {
return this.srNo;
}
public void setSrNo(Long srNo) {
this.srNo = srNo;
}
@Temporal(TemporalType.DATE)
@Column(name = "test_date", nullable = false, length = 13)
public Date getTestDt() {
return this.testDt;
}
public void setTestDt(Date testDt) {
this.testDt = testDt;
}
@Column(name = "test_time", nullable = false, length = 20)
public String getTestTime() {
return this.testTime;
}
public void setTestTime(String testTime) {
this.testTime = testTime;
}
你不能使用'*',因为Hibernate 正在转动对象。
如果要使用 *,请使用 @NamedNativeQuery
而不是 @NamedQuery
@NamedQueries({
@NamedQuery(name="slots",query = "SELECT mdl FROM Member mdl ")
})
// Use this
@NamedNativeQueries({
@NamedNativeQuery(name="slots_native",query = "SELECT * FROM Member mdl ",resultClass = MemberEntity.class)
})
JPQL 理解 java 个实体而不是列。
how to get same result without mentioning column names in select statement as if used *?
what's valid JPQL or HQL for select * from table query?
所以一个有效的 JQPL SELECT 查询甚至可以没有 SELECT
子句。
以下是对 return 来自 table
的完整实体的有效 JPQL 查询FROM tbl_slots mdl where mdl.test_date between :dt and :dt order by mdl.test_time asc
所以你的注释可以写成
@NamedQueries({
@NamedQuery(name="slots",query = "FROM tbl_slots mdl where mdl.test_date between :dt and :dt order by mdl.test_time asc")
})