class Mstatkey
// This defines the coomposite key over the columns deviceid, groupaddr, srcaddr
@Embeddable public final class MstatKey implements Serializable {
private Integer associatedNetDeviceId;
private Long groupAddr;
private Long sourceAddr;
....
// Here we use the composite key class Mstat
@NamedQuery( name=NamedQueryConstants.GET_ALLMSTATS_BY_STARTTIME_ENDTIME_DEVID_SRCADDR_GRPADDR, query="from Mstat mstat where mstat.timestamp >= :startTime and mstat.timestamp <= :endTime and " +"mstat.mstatkey.associatedNetDeviceId = :deviceId and " + "mstat.mstatkey.sourceAddr = :srcAddr and " + "mstat.mstatkey.groupAddr = :grpAddr order by mstat.timestamp )
@Entity
@Table(name = "MSTATS")
public final class Mstat implements Serializable {
@Id
@AttributeOverrides({
@AttributeOverride(name = "associatedNetDeviceId", column = @Column(name="DEVICE_ID")),
@AttributeOverride(name = "groupAddr", column = @Column(name="GROUP_ADDR")),
@AttributeOverride(name = "sourceAddr", column = @Column(name="SOURCE_ADDR"))
})
private MstatKey mstatkey;
@Column(name = "TIMESTAMP", nullable = false) private Date timestamp;
...
/// Now I use this query public List retrieveMstatByTimeRangeDevIdSrcAddrGrpAddr(Date startTime, Date endTime, Integer deviceId, Long srcAddr, Long grpAddr) throws HibernateSearchException {
Map<String, Object> queryParameters = new HashMap<String, Object>();
queryParameters.put("startTime", startTime);
queryParameters.put("endTime", endTime);
queryParameters.put("deviceId", deviceId);
queryParameters.put("srcAddr", srcAddr);
queryParameters.put("grpAddr", grpAddr);
return (List) executeNamedQuery(NamedQueryConstants.GET_ALLMSTATS_BY_STARTTIME_ENDTIME_DEVID_SRCADDR_GRPADDR, queryParameters);
// This is the actual Method public ListexecuteNamedQuery (final String queryName, final Map queryParameters) {
Session session = getHibernateUtil().getSession();
Query query = session.getNamedQuery(queryName);
for (Map.Entry<String, ?>e : queryParameters.entrySet()){
query.setParameter(e.getKey(), e.getValue());
// log.debug("KEY = " + e.getKey() + " VALUE == " + e.getValue());
}
return query.list();
// DataBase containms
TIMESTAMP DEVICE_ID SRC_ADDR PKTS_FWDED
2010-08-10 11:40:07.547 4 2887253771 3 2010-08-10 11:45:09.547 4 2887253771 18 2010-08-10 11:50:09.547 4 2887253771 19 /////////////////////////////////////////// //Hibernate (query.list) contents /////////////////////////////////////// TIMESTAMP = 2010-08-10 11:45:07.547 DEVICE_ID: 4 SRC_ADDR: 2887253771 PKTS_FWDED 3 TIMESTAMP = 2010-08-10 11:45:07.547 DEVICE_ID: 4 SRC_ADDR: 2887253771 PKTS_FWDED 3 <-- duplicate of the first entry TIMESTAMP = 2010-08-10 11:45:07.547 DEVICE_ID: 4 SRC_ADDR: 2887253771 PKTS_FWDED 3 <-- duplicate of the first entry