0

我有一个 MySQL 存储过程,它接受两个输入日期并返回EmpCodeNameDepartmentEmployee_Absent_DateTotal_No_OF_Days

存储过程(两个给定日期范围内的员工缺勤报告):

DELIMITER $$

DROP PROCEDURE IF EXISTS `AbsentReportproc`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `AbsentReportproc`(IN _fromdate DATETIME, IN _todate DATETIME)
BEGIN 
    CREATE TEMPORARY TABLE daterange25 (dte DATE); 

    SET @counter := -1;
    WHILE (@counter < DATEDIFF(DATE(_todate), DATE(_fromdate))) DO 
        INSERT INTO daterange25 VALUES (DATE_ADD(_fromdate, INTERVAL @counter:=@counter + 1 DAY));
    END WHILE;

SELECT tp.EMPCODE,tp.NAME,tp.DEPARTMENT, Group_Concat(d.dte order by d.dte SEPARATOR '\n')AbsentDate, COUNT(tp.EMPCODE) Totalnoofabsentdates
FROM test_prefixmaster tp
JOIN daterange25 d
LEFT JOIN test_prefixtransactions tpt ON (tp.EMPCODE = tpt.empcode) AND DATE(S_DateTime) = d.dte
WHERE tpt.empcode IS NULL
GROUP BY tp.EMPCODE;   

    DROP TABLE daterange25;
END$$

DELIMITER ;

这是我的休眠映射文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="edu.model.Master" table="test_prefixmaster">
        <id name="empcode" column="EMPCODE" length="10" ></id>
        <property name="dept">
            <column name="DEPARTMENT" length="30" />
        </property>
        <property name="empname">
            <column name="NAME" length="30" />
        </property>
    </class>
    <sql-query name="AbsentReportproc" callable="true">
    <return alias="master" class="edu.model.Master">
        <return-property name="empcode" column="EMPCODE" />
        <return-property name="dept" column="DEPARTMENT" />
       <return-property name="empname" column="NAME" /> 

       </return>
        <![CDATA[CALL AbsentReportproc(:_fromdate,:_todate)]]>
  </sql-query>
</hibernate-mapping>

这是我的测试类(主要):

public static void main(){
Query query = session.getNamedQuery("AbsentReportproc");
            query.setParameter("_fromdate", "2012-11-22");
            query.setParameter("_todate", "2012-11-23");
            List absentiesList=new ArrayList();
             absentiesList = query.list();

            for (int i = 0; i < absentiesList.size(); i++) {

                Master master = (Master) absentiesList.get(i);

        System.out.println("Employee Code:::" + master.getEmpcode());
        System.out.println("Employee Name:::"master.getEmpname());
   System.out.println("Employee Department:::" + master.getDept());         

            }
}

当我运行我的休眠代码时,我能够成功打印员工代码、姓名和部门

但我的实际要求是如何映射列并从名为 的临时表中打印 d.dte 的值daterange25?我应该为我的临时表创建一个 Pojo 吗?

谢谢

4

2 回答 2

0

我认为没有办法将 SP 的结果直接映射到实体。您应该编写并实例化您自己的 pojo,或者是的,您可以将实体映射到您的 SP 存储结果的临时表。

于 2013-01-16T07:12:16.917 回答
0

您可以根据 SP 结果的结构创建 POJO,并在 hibernate 映射 XML 中映射其列,而无需指定表名。不需要返回属性标签。

于 2015-08-04T17:24:43.517 回答