Hibernate和H2在内存中的软删除问题

时间:2017-03-13 07:28:33

标签: java hibernate jpa

我决定尝试this article中描述的软删除机制。 我的域模型中有一个带有删除指示符的AbstractEntity:

@MappedSuperclass
abstract class AbstractEntity {
@Column(name = "deleted_ind", nullable = false)
Boolean deleted = Boolean.FALSE
}

还有一个Job实体:

@Entity
@Table(name = "job")
@SQLDelete(sql = "UPDATE job SET deleted_ind = true WHERE job_id = ?")
@Loader(namedQuery = "findJobById")
@NamedQuery(name = "findJobById", query =
    "SELECT j FROM Job j WHERE j.id = ? AND j.deleted = false")
@Where(clause = "deleted_ind = false")
class Job extends AbstractEntity {
@Id
@Column(name = "job_id", nullable = false)
String id
}

但是当我尝试使用h2 in-memory运行我的集成测试时,会发生异常:

Hibernate: UPDATE job SET deleted_ind = true WHERE job_id = ?
2017-03-13 10:24:13,719 [main] INFO  o.h.e.j.b.i.AbstractBatchImpl - HHH000010: On release of batch it still contained JDBC statements 
2017-03-13 10:24:13,720 [main] WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 90008, SQLState: 90008 
2017-03-13 10:24:13,720 [main] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Invalid value "2" for parameter "parameterIndex" [90008-192] 
Caused by: org.hibernate.exception.GenericJDBCException: could not delete: [com.tolledo.myapp.Job#a]
Caused by: org.h2.jdbc.JdbcSQLException: Invalid value "2" for parameter "parameterIndex" [90008-192]

你能帮忙解决这个问题吗?

更新: 正在使用简单的Spring数据JpaRepository,并且正在调用这个特定的测试'detete'方法。

最终更新: 哦,我刚才意识到我忘了添加'版本'来查询。有

@Version
@Column(name = "version", nullable = false)
Integer version

在我的AbstractEntity中,所以最终的SQLDELETE看起来像:

@SQLDelete(sql = "UPDATE job SET deleted_ind = true WHERE job_id = ? and version = ?")

1 个答案:

答案 0 :(得分:0)

以这种方式更改SQL

@SQLDelete(sql = "UPDATE job SET deleted_ind = 1 WHERE job_id = ?")

@Where(clause = "deleted_ind = 0")