Hibernate SqlResultSetMapping Same Result Repeats instead of Unique

755 views Asked by At

I've a POJO which is mapped to result of a Stored Procedure.

@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;
}
}

The stored procedure looks like this

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 

I call the named native query like this

List<?> resultSet = mySessionFactory
                .getCurrentSession()
                .getNamedQuery(
                        "Pojo.getNotification")
                .setCacheable(false).setCacheMode(CacheMode.REFRESH).list();

If I run the select query in TOAD, I see two distinct records. However, if I iterate through resultSet, I see only one record appearing twice:

 [getMemberId()=114, getProgramId()=111]
 [getMemberId()=114, getProgramId()=111] 

Don't know, if the mapping is wrong or I need to implement something. BTW, I have the hashcode and equals method also defined in the POJO using the two properties.

I'm using Hibernate 4.2.7 with ehcache. The Database is DB2. The framework is Spring/Camel.

1

There are 1 answers

0
Kabira Speaking On BEST ANSWER

I've found it to be a issue with my @Id annotation. It was on member, which does get repeated. While the program Ids don't get repeated and are unique. So, when I put the @Id annotation on program instead, it's behaving as expected. Hurray!!