gpt4 book ai didi

java - Hibernate - 软删除 - 多个参数

转载 作者:行者123 更新时间:2023-12-01 09:12:17 27 4
gpt4 key购买 nike

当我尝试根据 @SqlDelete 注解中的多个参数“删除”行时,我遇到了软删除问题。

这是我的代码:

@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 {
...
}

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

Caused by: org.hibernate.exception.GenericJDBCException: could not execute statement at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211) at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62) at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3400) at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3630) at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:114) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:465) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:351) at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350) at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56) at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1258) at 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) at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:77) at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517) ... 136 common frames omitted Caused by: java.sql.SQLException: Statement parameter 2 not set. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860) at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1158) at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:780) at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073) at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009) at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5094) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994) at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java)

有人知道如何解决这个问题吗?
预先感谢您。

最佳答案

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

基于主键以外的其他内容删除数据不是一个好主意。因此,如果您只使用以下内容,它就会像魅力一样发挥作用。

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

关于java - Hibernate - 软删除 - 多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40844773/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com