HQL仅从多个结果中提取一个结果

时间:2016-08-15 17:15:41

标签: java sql-server hibernate hql

我正在运行 HQL 查询以从 MS SQL Server 获取结果。 hibernate生成的SQL给出了正确的输出(例如,2个结果),但Hibernate只提取一个结果。即使在堆栈跟踪中,它也显示查询返回了一个结果。我尝试过使用desc和asc的命令,但它总是得到ID值最低的那个。我看过我的模型,但似乎没有错。

我正在使用createquery("my query").list()方法。有人遇到过同样的问题吗?

createquery("from Analysis a where a.sample = *someNumber* ").list()

当我在数据库上运行时,HQL生成的SQL会给出正确的结果

select analysis0_.idAnalysis as idAnalys1_5_, analysis0_.changes_timestamp as changes_2_5_, analysis0_.Analysis_Done_Date as Analysis3_5_, analysis0_.Analysis_Remarks as Analysis4_5_, analysis0_.Sample_idSample as Sample_i5_5_, analysis0_.Analysis_Start_Date as Analysis6_5_, analysis0_.Analysis_Status as Analysis7_5_ from Re_at_AIM_User.analysis analysis0_ where analysis0_.Sample_idSample=*someNumber*

public class Analysis实现Serializable {

private static final long serialVersionUID = -1216945678610624994L;

@Column(name = "changes_timestamp", columnDefinition = "datetime")
@Temporal(TemporalType.TIMESTAMP)
private Date changesTimestamp;


@Column(name = "Analysis_Done_Date", columnDefinition = "datetime")
@Temporal(TemporalType.TIMESTAMP)
private Date doneDate;

@Id
@Column(name = "idAnalysis", columnDefinition = "bigint")
private Integer id;

@Column(name = "Analysis_Remarks")
private String remarks;

@Column(name = "Analysis_Start_Date", columnDefinition = "datetime")
@Temporal(TemporalType.TIMESTAMP)
private Date startDate;

@Column(name = "Analysis_Status")
private String status;

@ManyToOne()
@JoinColumn(name = "Sample_idSample")
private Sample sample;

public Analysis() {
}

public Date getChangesTimestamp() {
    return changesTimestamp;
}



public Date getDoneDate() {
    return doneDate;
}

public Integer getId() {
    return id;
}


public String getRemarks() {
    return remarks;
}

public Date getStartDate() {
    return startDate;
}

public String getStatus() {
    return status;
}

public void setChangesTimestamp(Date changesTimestamp) {
    this.changesTimestamp = changesTimestamp;
}



public void setDoneDate(Date doneDate) {
    this.doneDate = doneDate;
}

public void setId(Integer idAnalysis) {
    this.id = idAnalysis;
}

public void setRemarks(String remarks) {
    this.remarks = remarks;
}

public void setStartDate(Date startDate) {
    this.startDate = startDate;
}

public void setStatus(String status) {
    this.status = status;
}

public Sample getSample() {
    return sample;
}

public void setSample(Sample sample) {
    this.sample = sample;
}

}

2 个答案:

答案 0 :(得分:0)

Hibernate方法list() is deprecated

改为使用getResultList()

答案 1 :(得分:0)

这是一个可怕的错误。正在使用测试数据库。正式是最愚蠢的第一个问题。道歉。