spring - Hibernate exception - hibernate.internal.QueryImpl cannot be cast -
i'm trying authenticate database username. far error is:
login attempt not successful, try again. reason: org.hibernate.internal.queryimpl cannot cast com.**.**.model.userentity
the query in dao class
@repository public class userentitydaoimpl implements userentitydao{ @autowired private sessionfactory sessionfactory; public void setsessionfactory(sessionfactory sessionfactory) { this.sessionfactory = sessionfactory; } public session getcurrentsession() { return this.sessionfactory.getcurrentsession(); } @override public userentity getuserbyname(string username) { // todo auto-generated method stub userentity userentity = (userentity) sessionfactory.getcurrentsession().createquery( "select u userentity u u.username = '' + username + ''"); return userentity; }
service
@service("customuserdetailsservice") public class customuserdetailsservice implements userdetailsservice{ @autowired private userentitydao userentitydao; @autowired private assembler assembler; @override @transactional(readonly = true) public userdetails loaduserbyusername(string username) throws usernamenotfoundexception { // todo auto-generated method stub userdetails userdetails = null; userentity userentity = userentitydao.getuserbyname(username); if (userentity == null) throw new usernamenotfoundexception("user not found"); return assembler.builduserfromuser(userentity); } }
db table holds user details
/*table structure table `user` */ create table `user` ( `user_id` int(11) not null auto_increment , `name` varchar(45) null default null , `password` varchar(45) not null , `username` varchar(45) not null , `active` tinyint(1) not null , primary key (`user_id`) ) engine=innodb auto_increment=3 default charset=latin1;
model
@entity @table(name = "user", schema = "") @component public class userentity implements serializable { private static final long serialversionuid = 1l; @id @generatedvalue(strategy = generationtype.identity) @basic(optional = false) @column(name = "user_id") private integer id; @column(name = "name") private string name; @basic(optional = false) @column(name = "password") private string password; @basic(optional = false) @column(name = "username") private string username; @basic(optional = false) @column(name = "active") private boolean active; @jointable(name = "user_role", joincolumns = { @joincolumn(name = "user_id")}, inversejoincolumns = { @joincolumn(name = "role_id")}) @onetomany private set <role> roles; public userentity() { } //getters , setters
what insight on why there problem query, , why username not able retrieved database.
edit: after changing query, login still not successful. login page returned , there no error message in output console other this:
hibernate: select userentity0_.user_id user1_1_, userentity0_.active active1_, userentity0_.name name1_, userentity0_.password password1_, userentity0_.username username1_ user userentity0_ userentity0_.username=? hibernate: select roles0_.user_id user1_1_1_, roles0_.role_id role2_2_1_, role1_.role_id role1_0_0_, role1_.role role0_0_ user_role roles0_ inner join role role1_ on roles0_.role_id=role1_.role_id roles0_.user_id=? info : com.**.**.controller.applicationcontroller - login page {}.
you forgot execute query created. should be:
sessionfactory.getcurrentsession().createquery(...).uniqueresult();
also, use proper bind variable. stands query bogus single quotes, i'm not sure if made typo pasting stackoverflow, safer:
sessionfactory .getcurrentsession() .createquery("select u userentity u u.username = :username") .setparameter("username", username) .uniqueresult();
Comments
Post a Comment