Hibernate HQL delete with and

前端 未结 4 2075
名媛妹妹
名媛妹妹 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;
            }
    

提交回复
热议问题