我有一个 POJO,它映射到存储过程的结果。
@NamedNativeQueries( {
@NamedNativeQuery(name="Pojo.getNotification",
query="call SP_GET_NOTIFICATIONS()",
resultSetMapping = "notificationMapping",
resultClass = Pojo.class,
hints = {@QueryHint(name="org.hibernate.cacheable", value="false")}
)
}
)
@SqlResultSetMapping(name="notificationMapping",
entities = {
@EntityResult(
entityClass = Pojo.class,
fields={
@FieldResult(name="memberId", column="MBR_ID"),
@FieldResult(name="programId", column="PROG_ID")
}
)
}
)
@Entity
public class Pojo {
@Id
private Integer memberId;
private Integer programId;
public Integer getMemberId() {
return memberId;
}
public void setMemberId(Integer memberId) {
this.memberId = memberId;
}
public Integer getProgramId() {
return programId;
}
public void setProgramId(Integer programId) {
this.programId = programId;
}
}
存储过程如下所示
CREATE PROCEDURE SCHEMA.SP_GET_NOTIFICATIONS()
LANGUAGE SQL
DYNAMIC RESULT SETS 1
BEGIN
DECLARE C2 CURSOR WITH HOLD WITH RETURN TO CLIENT FOR
SELECT DISTINCT MBR.MBR_ID AS "MBR_ID",
PROG.MBR_PGM_ID AS "PROG_ID"
FROM SCHEMA.MEMBER_TBL MBR, SCHEMA.MEMBER_PGM_TBL PROG where MBR.SOMETHING = PROG.SOMETHING;
OPEN C2;
END
我这样称呼命名的本机查询
List<?> resultSet = mySessionFactory
.getCurrentSession()
.getNamedQuery(
"Pojo.getNotification")
.setCacheable(false).setCacheMode(CacheMode.REFRESH).list();
如果我在 TOAD 中运行选择查询,我会看到两条不同的记录。但是,如果我遍历 resultSet,我只会看到一条记录出现两次:
[getMemberId()=114, getProgramId()=111]
[getMemberId()=114, getProgramId()=111]
不知道,如果映射错误或者我需要实现一些东西。顺便说一句,我还使用这两个属性在 POJO 中定义了 hashcode 和 equals 方法。
我正在使用带有 ehcache 的 Hibernate 4.2.7。数据库是 DB2。框架是 Spring/Camel。