0
votes

I have problem with soft delete when I'm trying to "delete" rows based on multiple parameters in @SqlDelete anotation.

Here is my code:

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

When I run this code I got an error:

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)

Does anyone have any idea how to fix this?
Thank you in advance.

2

2 Answers

1
votes

The error is thrown because you have more than one parameter in @SQLDelete annotation. There is no way to set parameters of @SQLDelete query. Hibernate internally picks up the value passed during the session.delete call.

It is not a good idea to delete a data based on something other than primary key. So, if you just use the following it will work like a charm.

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

You can use @Where along with @SqlDelete but then the condition u define in where would be used to execute all queries. Not sure how can we make it particular to @SqlDelete.