使用CriteriaQuery和EntityManager(Hibernate)进行查询

时间:2017-05-07 10:55:21

标签: hibernate jpa entitymanager

如何使用CriteriaQuery和EntityManager为此SQL查询创建查询:

SELECT * FROM user WHERE user.login = '?' and user.password = '?' 

我试试:

        final CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
        final CriteriaQuery<User> criteriaQuery = criteriaBuilder.createQuery(User.class);
        Root<User> root = criteriaQuery.from(User.class);
        criteriaQuery.select(root);
        criteriaQuery.where(criteriaBuilder.gt(root.get("login"), userLogin));
        return getEntityManager().createQuery(criteriaQuery).getResultList().get(0);

1 个答案:

答案 0 :(得分:0)

这对我来说最终是这样的:

{{1}}