Franek Franek - 1 year ago 158
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);"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 Source

Why FROM USERS u instead of FROM USER u

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download