休眠 - 软删除 - 多个参数

时间:2016-11-28 12:49:11

标签: java spring hibernate

我在尝试"删除"时遇到软删除问题。基于@SqlDelete anotation中的多个参数的行。

这是我的代码:

@Entity
@Table(name = "department")
@DynamicInsert
@DynamicUpdate
//Override the default Hibernation delete and set the deleted flag rather than deleting the record from the db.
@SQLDelete(sql="UPDATE department SET valid_to = NOW(), active_f = 0 WHERE id = ? OR parent_department_id = ?")
//Filter added to retrieve only records that have not been soft deleted.
@Where(clause="valid_to IS NULL and active_f = 1")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Department implements Serializable {
    ...  
}

当我运行此代码时出现错误:

  

引起:org.hibernate.exception.GenericJDBCException:不能   执行声明         在org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)         在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)         在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)         at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)         在org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)         在org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3400)         在org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3630)         在org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:114)         在org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:465)         在org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:351)         在org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)         在org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)         在org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1258)         在org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)         at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)         at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)         在org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:77)         在org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517)         ...省略了136个常用帧       引起:java.sql.SQLException:未设置语句参数2。         在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)         在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)         在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885)         在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)         在com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1158)         在com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:780)         在com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)         at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)         在com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5094)         在com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)         在com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61)         在com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java)

有没有人知道如何解决这个问题? 提前谢谢。

2 个答案:

答案 0 :(得分:1)

抛出错误是因为@SQLDelete注释中有多个参数。无法设置@SQLDelete查询的参数。 Hibernate在内部获取session.delete调用期间传递的值。

基于主键以外的其他内容删除数据并不是一个好主意。所以,如果您只是使用以下内容,它将像魅力一样工作。

@SQLDelete(sql="UPDATE department SET valid_to = NOW(), active_f = 0 WHERE id = ?")

答案 1 :(得分:0)

您可以将@Where与@SqlDelete一起使用,但是条件u定义将用于执行所有查询的位置。不知道我们怎样才能让它特别适合@SqlDelete。