我有一个带有逻辑删除字段的表 -
CREATE TABLE DOCUMENT
(
ID VARCHAR2(255 BYTE) NOT NULL,
DOCUMENT_NAME VARCHAR2(255 BYTE),
DELETED NUMBER
)
我创建了JPA对象 -
@Entity(name = "DOCUMENT")
public class Document {
@Column(name = "ID")
private String id;
@Column(name = "DOCUMENT_NAME")
private String name;
@Column(name = "DELETED")
private Boolean deleted;
// other params
}
使用存储库 -
public interface DocumentRepository extends CrudRepository<Document, String> {
Document findByIdAndDeleted(String id, Boolean deleted);
}
我只想查找未删除的文档,所以想知道是否有办法将我的查询默认为deleted = false,并允许我进行存储库搜索,如
// where deleted = false
Document findById(String id);
答案 0 :(得分:3)
答案 1 :(得分:1)
如果您使用Hibernate,可以在课程中添加@Where
注释。像这样:
@Where(clause="deleted <> '1'")
@Entity(name = "DOCUMENT")
public class Document {
@Column(name = "ID")
private String id;
@Column(name = "DOCUMENT_NAME")
private String name;
@Column(name = "DELETED")
private Boolean deleted;
// other params
}
看看http://featurenotbug.com/2009/07/soft-deletes-using-hibernate-annotations/。