hibernate批量删除写法
1.批量删除正常写法,此方式在海量数据删除时可能有效率问题
private static final String DELETE_USER_NAME= "DELETE FROM User x WHERE x.name in (:usernames) and x.id = userId"; @Override public boolean deleteUserName(List<String> users, Integer userID) { if (username!= null && !username.isEmpty()) { Number result = em.createQuery(DELETE_USER_NAME) .setParameter("usernames",users) .setParameter("id", userID) .executeUpdate(); return ((result.intValue() == 1) ? true : false); } else return false; }
参考:https://forum.hibernate.org/viewtopic.php?f=1&t=1044044
测试发现,以上代码有问题,正确写法:
.setParameter("usernames",users)应改成.setParameterList("usernames",users)
hibernate-api:https://docs.jboss.org/hibernate/orm/5.0/javadocs/