doniyor
doniyor

Reputation: 37914

[PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query]

i need help. i am trying to select from database thru sql statement in play framework, but it gives me error, i cannot figure out where the clue is. here is the code:

@Transactional
public static Users findByUsernameAndPassword(String username, String password){
String hash = DigestUtils.md5Hex(password);
Query q = JPA.em().createNativeQuery("select * from USERS where" + "USERNAME=? and PASSWORD=?").setParameter(1, username).setParameter(2, password);
List<Users> users = q.getResultList();
if(users.isEmpty()){
    return null;
} else{
    return users.get(0);

here is the eror message:

[PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query]

can someone help me please! any help i would appreciate!

thanks

Upvotes: 0

Views: 3143

Answers (3)

aaberg
aaberg

Reputation: 2273

You are missing some spaces in your query. Try to fix that:

"select * from USERS where USERNAME=? and PASSWORD=?"

Upvotes: 3

Avinav Mishra
Avinav Mishra

Reputation: 758

I also faced the same problem and replaced the @GeneratedValue from the id and manually put the value of the id it is now working.

Upvotes: 0

Alex
Alex

Reputation: 140

You are using pure sql in a ORM (object-relational mapping) query. JPA lets you build queries with your objects and it's properties, not your database tables.

TypedQuery<Users> q = JPA.em().createQuery("select * from Users where username= :username and password= :password", Users.class);
q.setparameter("username", username);
q.setparameter("password", hash);
List<Users> users = q.getResultList();

That is if your User entity is named Users, it probably is User though.

Upvotes: 3

Related Questions