Example of DAO implementation for Hibernate Criteria Query -> Find By Id
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
... @Repository public class JpaModelDaoImpl implements JpaModelDao { @PersistenceContext private EntityManager em; ... @Override public ModelEntity findById (int modelId) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<ModelEntity> cq = cb.createQuery(ModelEntity.class); Root<ModelEntity> model = cq.from(ModelEntity.class); cq.where(cb.equal(model.get("id"), modelId)); TypedQuery<ModelEntity> q = em.createQuery(cq); return q.getSingleResult(); } ... } |
Model class has attribute: id