gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:41:14 26 4
gpt4 key购买 nike

我决定尝试 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 运行集成测试时,出现异常:

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 = ?")

最佳答案

以这种方式更改 SQL

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

@Where(clause = "deleted_ind = 0")

关于java - Hibernate 和 H2 内存中的软删除问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42758370/

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