我有MySQL数据库表,其中包含以下字段和数据:
这里的id是主键,它是自动生成的。我的查询是通过projectId获取成员。我在Hibernate中的查询是:
Query query=session.createQuery("from ProjectMemberDTO P where P.projectId=:pid");
query.setParameter("pid", pid);
假设pid是49然后我得到了表的第一个结果,因为有多少pid,即我得到
harindermo
harindermo
harindermo
但是当我在MYSQL WorkBench中运行此查询时,它会给出正确的结果。
@Entity
@Table(name="projectmember")
public class ProjectMemberDTO {
@Id
@Column(name="projectId")
int projectId;
@Column(name="id")
int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="member")
String member;
@Column(name="status")
String status;
public int getProjectId() {
return projectId;
}
public void setProjectId(int projectId) {
this.projectId = projectId;
}
public String getMember() {
return member;
}
public void setMember(String member) {
this.member = member;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
这是我的ProjectMemberDTO
答案 0 :(得分:0)
您可以很好地编写本机SQL查询,如下所示:
String sql = "SELECT * FROM ProjectMemberDTO WHERE projectId = :projectId";
SQLQuery query = session.createSQLQuery(sql);
query.setParameter("projectId", projectId);
List results = query.list();