Hibernate HQL delete with and

前端 未结 4 2077
名媛妹妹
名媛妹妹 2021-02-12 22:45

Hibernate doesn\'t delete my row:

public boolean deleteVote(Login user, int pid){

      Session session = getSession();

      try{
          String hql = \"del         


        
4条回答
  •  猫巷女王i
    2021-02-12 23:14

    You need to begin and commit a transaction.

    Transaction transaction = session.beginTransaction();
    try {
      // your code
      String hql = "delete from Vote where uid= :uid AND pid= :pid";
      Query query = session.createQuery(hql);
      System.out.println(user.getUid() + " and pid: " + pid);
      query.setString("uid", user.getUid());
      query.setInteger("pid", pid);
      System.out.println(query.executeUpdate());
      // your code end
    
      transaction.commit();
    } catch (Throwable t) {
      transaction.rollback();
      throw t;
    }
    

    It is also possible that you need to close the session before the changes will be visible in the database.

提交回复
热议问题