Aşağıdaki gibi iki tablonun birleştirildiği (join) bir distinct sorgu düşünelim. Ayrıca sorgu bir sütuna göre de filtrelensin:
SELECT DISTINCT ud.birthDate FROM user u, userDetail ud WHERE u.id=ud.userId AND u.membershipType=?
Bunun hibernatedeki karşılığını veren fonksiyon aşağıdaki gibi olmalı:
public List<Date> findDistinctBirthDates(String membershipType) {
try {
Session session = getSession();
Criteria userCriteria = session.createCriteria(User.class);
Criteria userDetailCriteria =
userCriteria .createCriteria("userDetail","ud");
userDetailCriteria.setProjection(
Projections.distinct(
Projections.property("ud.birthDate")
)
);
userCriteria.add(Restrictions.eq("membershipType",membershipType));
return userCriteria.list();
} catch (HibernateException e) {
throw new DBInfrastructureException(e);
}
}
try {
Session session = getSession();
Criteria userCriteria = session.createCriteria(User.class);
Criteria userDetailCriteria =
userCriteria .createCriteria("userDetail","ud");
userDetailCriteria.setProjection(
Projections.distinct(
Projections.property("ud.birthDate")
)
);
userCriteria.add(Restrictions.eq("membershipType",membershipType));
return userCriteria.list();
} catch (HibernateException e) {
throw new DBInfrastructureException(e);
}
}
Hiç yorum yok:
Yorum Gönder