Franek Franek - 4 months ago 41
Java Question

createQuery with JPQL hangs

When I call createQuery(String, Class) application hangs. Hibernate last INFO is "Using ASTQueryTranslatorFactory" but then nothing happens. Here is the code:

public User getUserByLogin(String username) {
String queryString = "SELECT u FROM USERS u WHERE u.USERNAME = :username";
TypedQuery<User> query = entityManager.createQuery(queryString, User.class);
return query.setParameter("username", username).getSingleResult();
}


It hangs on
TypedQuery<User> query = entityManager.createQuery(queryString, User.class);


But what is funny, when I change method body like this:

public User getUserByLogin(String username) {
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<User> criteriaQuery = criteriaBuilder.createQuery(User.class);
Root<User> userRoot = criteriaQuery.from(User.class);
criteriaQuery.select(userRoot).where(criteriaBuilder.equal(userRoot.get("username"), username));
TypedQuery<User> query = entityManager.createQuery(criteriaQuery);
return query.getSingleResult();
}


it works fine. Same environment, just different body of methos. What Am I missing?

Answer

Why FROM USERS u instead of FROM USER u

Comments