0

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

3 Answers 3

3

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.

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

5 Comments

thanks, now i get this error: PersistenceException: [PersistenceUnit: defaultPersistenceUnit] Unable to configure EntityManagerFactory
Is Users your @Entity ? What do you have in it ? (Plural is usualy for admin controllers, that's why it is odd to find a model called Users). Did you try using Users.find("etc etc") ?
yeah, my User is @Entity. I have renamed it back to 'User'. but i get the same error again. the problem is that it cannot find my Entity class, right?
I'm not sure if that's the real problem, now that I'm sure User is your entity, you can try : User.find("") This code is like Select * From User Where... so in your case it could be like this... User user = User.find("username = ? AND password = ?", username, hash).first()
Also, just a sidenote, you are saving in database using sql, when you could just User user = new User(blablabla) and then user.save()
3

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

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

3 Comments

dont i have to do like this: "select * from USERS where USERNAME='?' and PASSWORD='?'" so that the username will stay in '' ?
No. Parameters will automatically be inserted, so they are interpreted as the right datatype. A varchar or char parameter, may not be surrounded with ''
ok thanks it is working now, i had different problem, and now i have different problem :D. anyway, thanks a lot
0

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.

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.