Hibernate不会删除我的行:
public boolean deleteVote(Login user, int pid){
Session session = getSession();
try{
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());
}catch(Exception e){
Outprint:
uid: 123 and pid: 1
Hibernate: delete from votes where uid=? and pid=?
1
当我直接在SQL中尝试时,SQL语法正在运行。直接SQL语法:
delete from votes where uid= '123' AND pid= 1
映射:
<class name="package.model.Vote" table="votes">
<id name="vid" column="vid" >
<generator class="increment"/>
</id>
<property name="pid" column="pid" />
<property name="uid" column="uid" />
<property name="tid" column="tid" />
<property name="votes" column="votes" />
</class>
表:
CREATE TABLE IF NOT EXISTS `votes` (
`vid` int(11) NOT NULL
`pid` int(11) NOT NULL,
`uid` varchar(20) NOT NULL,
`tid` int(11) NOT NULL,
`votes` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`vid`),
KEY `pcid` (`pid`,`uid`),
KEY `uid` (`uid`),
KEY `tid` (`tid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;
ALTER TABLE `votes`
ADD CONSTRAINT `votes_ibfk_3` FOREIGN KEY (`pid`) REFERENCES `poll` (`pid`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `votes_ibfk_4` FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `votes_ibfk_5` FOREIGN KEY (`tid`) REFERENCES `teams` (`tid`) ON DELETE CASCADE ON UPDATE CASCADE;
INSERT INTO `votes` (`vid`, `pid`, `uid`, `tid`, `votes`) VALUES
(20, 1, '123', 1, 1);
我想这很简单,因为到目前为止一切看起来都不错。我没有任何错误或其他任何错误,只是没有删除。
感谢任何帮助。
答案 0 :(得分:33)
您需要开始并提交交易。
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;
}
在更改在数据库中可见之前,您还可能需要关闭会话。
答案 1 :(得分:1)
来自您提供的输出
uid: 123 and pid: 1
Hibernate: delete from votes where uid=? and pid=?
1
很明显query.executeUpdate()
正在返回1.方法返回
更新或删除的实体数量。这意味着已经更新或删除了1行,这没关系。
尝试执行session.flush()
来刷新会话,或尝试session.evict()
从会话中删除对象。
答案 2 :(得分:0)
带有HQL和session.update的完整Java代码
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;
}
答案 3 :(得分:0)
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;
答案 4 :(得分:-1)
entityManager.createQuery(“DELETE FROM TableName”)。executeUpdate();