java - update mysql table with hibernate showing error -
in spring project want update mysql table field according url :
i have url below:
localhost:9191/access/name/article?key=xyz
i want fetch article url , update status , article field of corrsponding mysql table
in database have table name "user".
user(stu_id,name,email,article,status)
mysql query is:
update user set article='null', status=true article='xyz'; here xyz=user.getarticle()
to achieve have done below
user.java is:
public user(string article, string status) { super(); this.article = article; this.status = status; }
userdao.java
public interface userdao { public void updateuser(user user); }
userdaoimpl.java is:
@transactional @repository("userdao") public class userdaoimpl implements userdao { @autowired private sessionfactory sessionfactory; public void updateuser(user user) { string hql = "update user set article = null,status=true" +"where article=:key1"; sessionfactory.getcurrentsession().createquery(hql) .setparameter("key1", user.getarticle()); } }
userservice.java is:
public interface userservice { user updateuser(string article, string status); }
userserviceimpl.java is:
@service("userservice") @transactional(propagation = propagation.supports) public class userserviceimpl implements userservice { public user updateuser(string article, string status) { user user = new user(article,status); userdao.updateuser(user); return user; return user; }
usercontroller.java is:
//localhost:9191/access/name/article?key=xyz @requestmapping(value="/access/name/id", method=requestmethod.get) public @responsebody string byparameter( user user, httpservletrequest request) { boolean ps=true; string foo= request.getparameter("key"); userservice.updateuserinfo(foo, ps); return "signuplogin"; }
but showing error:
error [] (errorcounter.java:56) - line 1:51: unexpected token: key error [] (errorcounter.java:56) - line 1:58: unexpected token: =
java.lang.illegalargumentexception: node traverse cannot null!
at org.hibernate.impl.sessionimpl.createquery(sessionimpl.java:1760) @ com.student.dao.userdaoimpl.updateuser(userdaoimpl.java:40) @ com.student.service.userserviceimpl.updateuserinfo(userserviceimpl.java:66)
where problem?what doing wrong??
@ com.student.dao.userdaoimpl.updateuser(userdaoimpl.java:40)
you executing wrong query.you should below code:
@transactional @repository("userdao") public class userdaoimpl implements userdao { @autowired private sessionfactory sessionfactory; public void updateuser(user user) { string hql = "update user set article = null,status=true" +"where article=:key1"; sessionfactory.getcurrentsession().createsqlquery(hql) .setparameter("key1", user.getarticle()); } }
the syntax like: user(this database table ) , article,status fields
Comments
Post a Comment