Hibernate Query只返回一个值

时间:2017-06-05 13:43:02

标签: spring hibernate hql oracle-sqldeveloper

我正在尝试创建一个Spring Hibernate App并且我正在访问一个具有以下值的表:

select REPAIR_STATUS_CD,STATUS_DATE 
from repair_status 
where REPAIR_CONF_NO ='1234567';

REPAIR_STATUS_CD STATUS_DATE          
----- -------------------- - 
NEWO  25-FEB-2016 20:07:45 
RLSD  25-FEB-2016 20:07:45 
REQA  25-FEB-2016 20:13:24 
URCD  26-FEB-2016 19:43:40 
UINS  26-FEB-2016 19:43:45 
UBER  27-FEB-2016 09:42:59 
RQT6  27-FEB-2016 09:46:28 
RQXP  03-MAR-2016 12:24:43 
RBER  04-MAR-2016 21:52:22 
SPCM  09-MAY-2016 18:13:33 
SCOM  10-MAY-2016 19:09:54 

现在在DAOImpl类中,我使用下面的代码获取相同内容。

Session session = sessionFactory.getCurrentSession();

            session.beginTransaction();

            Query<RepairStatus> query = session.createQuery("from     RepairStatus where repairConfNo = '"+repairConfNo+"' ");
            List<RepairStatus> repairStatusList = query.getResultList();    
            System.out.println("Inside Repair Status Service");
            for(RepairStatus repairStatus: repairStatusList){
                System.out.println(repairStatus.getRepairStatusCode());
            }

但是当我使用sysout和for循环打印输出时,我得到以下内容:

内部维修状态服务

NEWO
NEWO
NEWO
NEWO
NEWO
NEWO
NEWO
NEWO
NEWO

尽管数据库具有正确的值,但为什么行会重复。

提前多多感谢:)

这也是我的实体类

package com.gsx.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="TG_REPAIR_STATUS")
public class RepairStatus {

@Id
@Column(name="REPAIR_CONF_NO")
private String repairConfNo;

@Column(name="REPAIR_STATUS_CD")
private String repairStatusCode;

@Column(name="STATUS_DATE")
private Date statusDate;

public void setStatusDate(Date statusDate) {
    this.statusDate = statusDate;
}

public String getRepairConfNo() {
    return repairConfNo;
}

public void setRepairConfNo(String repairConfNo) {
    this.repairConfNo = repairConfNo;
}

public Date getStatusDate() {
    return statusDate;
}

public String getRepairStatusCode() {
    return repairStatusCode;
}

public void setRepairStatusCode(String repairStatusCode) {
    this.repairStatusCode = repairStatusCode;
}

}

1 个答案:

答案 0 :(得分:0)

你应该替换

Query<RepairStatus> query = session.createQuery("from     RepairStatus where repairConfNo = '"+repairConfNo+"' ");

Query<RepairStatus> query = session.createQuery("from     RepairStatus where repairConfNo =:repairConfNo");
parameterQuery.setParameter("repairConfNo", you_repairConfNo);
List<RepairStatus> repairStatusList = query.getResultList(); 

查询看起来不错,您需要启用show_sql

如果你正在使用hibernate.cfg.xml,那么你可以启用像

这样的sql
<property name="show_sql">true</property>

然后通过hibernate调试生成的sql,它可以帮助你找出问题。

此外,您的表格名称为 TG_REPAIR_STATUS

@Table(name="TG_REPAIR_STATUS")
public class RepairStatus

并且您正在运行查询

select REPAIR_STATUS_CD,STATUS_DATE 
from repair_status 
where REPAIR_CONF_NO ='1234567';
repair_status 表而不是 TG_REPAIR_STATUS

上,请在您的数据库中验证这一点