5

How create query with using CriteriaQuery and EntityManager for this SQL query:

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

I try so:

        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);

3 Answers 3

2

Your code looks like it's on the right track, except that it only has one WHERE condition, which does not agree with your raw SQL query, which has two conditions.

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<User> q = cb.createQuery(User.class);
Root<User> c = q.from(User.class);
q.select(c);
ParameterExpression<String> p1 = cb.parameter(String.class);
ParameterExpression<String> p2 = cb.parameter(String.class);
q.where(
    cb.equal(c.get("login"), p1),
    cb.equal(c.get("password"), p2)
);
return em.createQuery(q).getResultList().get(0);

As a side note, in real life you would typically not be storing raw user passwords in your database. Rather, you be storing a salted and encrypted password. So hopefully your actual program is not storing raw passwords.

Sign up to request clarification or add additional context in comments.

Comments

0

This worked for me in the end was this:

    entityManager.getTransaction().begin();

    CriteriaQuery<Object> criteriaQuery = criteriaBuilder.createQuery();
    Root<Profile> fromRoot = criteriaQuery.from(Profile.class);
    criteriaQuery.select(fromRoot);

    criteriaQuery.where(criteriaBuilder.equal(fromRoot.get("userName"), username),
                        criteriaBuilder.equal(fromRoot.get("password"), password));

    List<Object> resultList = entityManager.createQuery(criteriaQuery).getResultList();
    Profile dbProfile = null;
    if (resultList.isEmpty()) {
        // Handle Error
    } else {
        dbProfile = (Profile) resultList.get(0);
    }

    entityManager.getTransaction().commit();

Comments

0

In Where clause two conditions are present, we can use Predicate .


public List<User> findUserByLoginAndPassword(String login, String password){
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<User> cq = cb.createQuery(User.class);
Root<User> user = cq.from(User.class);

Predicate loginPredicate = cb.equal(user.get("login"), login);
Predicate passwordPredicate = cb.equal(user.get("password"), password);

//select * from User Where user.login=login and user.password=password;
cq.select(root).where(cb.and(loginPredicate, passwordPredicate));

TypedQuery<User> query = entityManager.createQuery(cq);
return query.getResultList().get(0);

}

Hope this will help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.