我有一个MySQL存储过程,接受两个输入日期并返回EmpCode
,Name
,Department
,Employee_Absent_Date
和Total_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 ;
这是我的Hibernate映射文件:
<?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());
}
}
当我运行Hibernate代码时,我能够成功打印员工代码,名称和部门
但我的实际要求是如何映射列并从名为daterange25
的临时表中打印d.dte的值?我应该为临时桌创建一个Pojo吗?
由于
答案 0 :(得分:0)
我认为没有办法将SP的结果直接映射到实体。您应该编写并实例化自己的pojo,或者是的,您可以将实体映射到SP存储结果的临时表。
答案 1 :(得分:0)
您可以根据SP结果的结构创建POJO,并在hibernate映射XML中映射其列,而不指定表名。不需要返回属性标记。