更新 mysql table 休眠显示错误

update mysql table with hibernate showing error

在我的 Spring 项目中,我想根据 url 更新 mysql table 字段:

我下面有url:

localhost:9191/access/name/article?key=xyz

我想从 url 中获取文章,然后更新相应 mysql 的状态和文章字段 table

在我的数据库中,我有 table 名称 "user"。

user(stu_id,name,email,article,status)

mysql查询是:

UPDATE user
SET article='null', status=true
WHERE article='xyz';  here xyz=user.getArticle()

为了实现这一点,我做了以下工作

User.java 是:

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 是:

@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 是:

public interface UserService {

    User updateUser(String article, String status);

}

UserServiceImpl.java 是:

@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 是:

//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";
    }

但显示错误:

ERROR [] (ErrorCounter.java:56) - line 1:51: unexpected token: key
ERROR [] (ErrorCounter.java:56) - line 1:58: unexpected token: =

java.lang.IllegalArgumentException: 遍历节点不能为空!

at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1760)
    at com.student.dao.UserDaoImpl.updateUser(UserDaoImpl.java:40)
    at com.student.service.UserServiceImpl.updateUserinfo(UserServiceImpl.java:66)

问题出在哪里?我哪里做错了??

对 sql 查询进行以下更正,使用 User 代替 user 查询。

String hql = "update User set article = null, status = true where article = :key1";

如果您不想更改此方法,则可以使用 createSqlQuery 方法代替 createQuery 方法。

此解决方案可能会有所帮助。

  at com.student.dao.UserDaoImpl.updateUser(UserDaoImpl.java:40)

你执行错了 query.you 应该像下面的代码那样做:

@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());
        }
    }

语法如下: 其中 user(这是数据库 table )和 article,status 是字段