I want to fetch rows with distinct batch code and id.
The below code is now fetching duplicate batch codes like:
batch1 12,
batch1 45,
batch1 63,
batch2 96,
batch2 96
@Entity
@Table(name = "key")
public class Key implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, length = 11)
@Column(name = "batch_code", nullable = false)
private String batchCode;
//getter , setter
}
Criteria c = getSession().createCriteria(Key.class);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("batchCode"));
c.setProjection(Projections.distinct(projList));
c.setProjection(Projections.property("id"));
if (searchTerm != null && !searchTerm.isEmpty()) {
c.add(Restrictions.like("keyCode", searchTerm.toUpperCase() + "%"));
}
c.setFirstResult(currPosition);
c.setMaxResults(pageSize);
List<Key> result = c. list();