我在Hibernate中绝对是新手,我有以下疑问。我有这个实体类,并且在我的类中,它被删除了2个JPQL命名查询:
@NamedQueries({
@NamedQuery(name = "kmProjectInfoStatusList", query = "select status from KM_ProjectInfoStatus status order by status.idProjectInfoStatus") ,
@NamedQuery(name = "kmProjectInfoStatusById", query = "SELECT status FROM KM_ProjectInfoStatus status where lower(status.idProjectInfoStatus) = :statusId")
})
@Entity
@Table(name = "KM_PROJECT_INFO_STATUS")
public class KM_ProjectInfoStatus implements Serializable {
@Id
@GeneratedValue
private Long idProjectInfoStatus;
@Column(name = "foldertech")
private Long foldertech;
@Column(name = "folderproject")
private Long folderproject;
public Long getIdProjectInfoStatus() {
return idProjectInfoStatus;
}
public void setIdProjectInfoStatus(Long idProjectInfoStatus) {
this.idProjectInfoStatus = idProjectInfoStatus;
}
public Long getFoldertech() {
return foldertech;
}
public void setFoldertech(Long foldertech) {
this.foldertech = foldertech;
}
public Long getFolderproject() {
return folderproject;
}
public void setFolderproject(Long folderproject) {
this.folderproject = folderproject;
}
}
所以我的实体类名为 KM_ProjectInfoStatus ,并映射 KM_PROJECT_INFO_STATUS 数据库表。
我的疑问主要与 JPQL命名查询的确切方式有关(我认为这是JPQL,是否正确?),考虑第一个查询
@NamedQuery(name = "kmProjectInfoStatusList", query = "select status from KM_ProjectInfoStatus status order by status.idProjectInfoStatus")
究竟如何运作?我认为所选的状态代表我的 KM_PROJECT_INFO_STATUS 数据库表的一行(因此它代表了映射它的 KM_ProjectInfoStatus 实体的特定实例)
如果它是正确的,这意味着,与标准SQL不同,在JPQL中我不是选择表字段的子集而是选择映射整个表行的实体吗?
TNX
答案 0 :(得分:1)
在JPQL中,我们使用类和变量来生成查询,而不是表名和列名。您将从Google获得有关此内容的详细信息。
select status from KM_ProjectInfoStatus status order by status.idProjectInfoStatus
在JPQL查询中,它正在检索所有状态对象,这是KM_ProjectInfoStatus类的对象,它在内部映射到表KM_PROJECT_INFO_STATUS。
因此,您的Hibernate驱动程序会将此JPQL查询转换为sql查询。有一些设置可以通过它们在控制台中打印这些sql查询来验证实际的sql查询。