java - The state field path cannot be resolved to a valid type -


first of all, have researched question still can't figure out why isn't working me.

i'm developing dynamic web application in java uses jpa store login information of users. have no trouble inserting new users or listing users table. i'm trying write login method authenticate users, in want select user given username , password.

here's method in daoimpl class:

public user login(string username, string password) {     string sqlcommand = string.format("select u users u u.uname = '%s' , u.password = '%s'", username, password);     query q = this.getentitymanager().createquery(sqlcommand);     //q.setparameter("uname", username);     //q.setparameter("pass", password);      try{         return (user) q.getsingleresult();     } catch(exception e) {         return null;     } } 

and here's entity class:

@entity(name=user.table_name) @table(name=user.table_name, schema=persistentobject.schema) public class user extends persistentobject{  public static final string table_name ="users";  public static final string first_name ="fname"; public static final string last_name = "lname"; public static final string username ="uname"; public static final string email = "email"; public static final string password = "password";  @column(name=first_name, nullable=false) private string firstname;  @column(name=last_name, nullable=false) private string lastname;  @column(name=username, nullable=false, unique=true) private string username;  @column(name=email, nullable=false, unique=true) private string email;  @column(name=password, nullable=false) private string password;  @onetomany(mappedby="user") private list<update> updatelist;  @onetomany(mappedby="user1") private list<friend> friendlist;  public user() { }  public user(string firstname, string lastname, string username,         string email, string password) {     super();     this.firstname = firstname;     this.lastname = lastname;     this.username = username;     this.email = email;     this.password = password; } 

and in servlet, i'm trying call login() method of daoimpl class this:

user loggedinuser = this.userdao.login(request.getparameter("uname"),   request.getparameter("pass")); 

and error @ same line:

java.lang.illegalargumentexception: exception occurred while creating query in entitymanager: <|exception description: problem compiling [select u users u u.uname = 'something' , u.password = '12345']. |[28, 35] state field path 'u.uname' cannot resolved valid type.|[57, 67] state field path 'u.password' cannot resolved valid type.

the weird thing don't error if try filter id or email columns. login successful then. every other column, above error.

the query you're executing not sql query. it's jpql query. jpql different language. in particular, never uses table , column names. uses entity names , mapped fields/properties names.

so query should

select u user u u.username = :name , u.password = :password 

note in addition injection attacks due usage of string.format() instead of named parameters, query won't work user name or password contains singla quote. storing passwords in clear-text in database not idea @ all. should salted , hashed.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -