Hibernate HQL delete with and

前端 未结 4 2062
名媛妹妹
名媛妹妹 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条回答
  • 2021-02-12 23:08

    full java code with HQL & session.update

            try {
    
                try (Session session = this.sessionFactory.openSession()) {
    
                    session.beginTransaction();
    
                    String hql = "UPDATE Employee SET " +
                            "firstName = :firstName, " +
                            "lastName = :lastName," +
                            "gender = :gender," +
                            "jobTitle = :jobTitle," +
                            "departmentId = :departmentId " +
                            "where employeeId = :employeeId";
    
                    Query query = session.createQuery(hql);
    
                    query
                            .setParameter("employeeId", employee.getEmployeeId())
                            .setParameter("departmentId", employee.getDepartmentId())
                            .setParameter("firstName", employee.getFirstName())
                            .setParameter("lastName", employee.getLastName())
                            .setParameter("gender", employee.getGender())
                            .setParameter("jobTitle", employee.getJobTitle())
                    ;
    
                    int rows = query.executeUpdate();
    
    //              session.update(Objects.requireNonNull(employee));
    
                    session.getTransaction().commit();
    
                    return session.get(Employee.class, employee.getEmployeeId()).equals(employee);
                }
    
            } catch (RuntimeException e) {
                e.printStackTrace();
                throw e;
            }
    
    0 讨论(0)
  • 2021-02-12 23:10
            int deletedRows = 0;
            try {
                session = sessionProvider.get();
                String hql = "delete from ApprovalRemark approvalRemark where approvalRemark.approvalItem.id =:approvalItemId ";
                Query query = session.createQuery(hql);
                query.setParameter("approvalItemId", approvalItemId);
                deletedRows = query.executeUpdate();
            } catch (javax.persistence.NoResultException e) {
                return false;
            } catch (HibernateException he) {
                log.error("deleteAllApprovalRemarksForApprovalItem exception", he);
                throw he;
            }
            log.debug("deleteAllApprovalRemarksForApprovalItem deleted " + deletedRows + " rows");
            return true;
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-12 23:20

    From the Output you provided

    uid: 123 and pid: 1
    Hibernate: delete from votes where uid=? and pid=?
    1
    

    it is clear that query.executeUpdate() is returning 1. The method returns the number of entities updated or deleted. This means that 1 row has been updated or deleted, which is okay.

    Try doing a session.flush() to flush the session, or a session.evict() to remove the object from the session.

    0 讨论(0)
提交回复
热议问题