gpt4 book ai didi

java - 如何忽略@Where hibernate

转载 作者:行者123 更新时间:2023-12-01 16:24:00 24 4
gpt4 key购买 nike

我有一个实体作者

@Data
@Entity
@DynamicInsert
@Where(clause = "isDeleted=false")
public class Author

和实体书

@Data
@Entity
@DynamicInsert
public class Book {
...

@OneToOne(cascade = {CascadeType.MERGE, CascadeType.REMOVE }, fetch = FetchType.EAGER,orphanRemoval = true)
@JoinColumn(name = "author_id", nullable = false )
private Author author;
}

即使作者被删除,我也需要获取所有书籍。我使用 JpaRepository:

public interface BookRepository extends JpaRepository<Book, Long>, JpaSpecificationExecutor {} 

但是当我尝试时

bookRepository.findAll()

我收到异常:javax.persistence.EntityNotFoundException:无法找到 ID 为 1 的作者

尝试过这个,但它对我没有帮助:

public interface BookRepository extends JpaRepository<Book, Long>, JpaSpecificationExecutor {

@Override
@Query(value = "from Book as b where b.author.isDeleted = true")
List<Book> findAll();

我怎样才能避免@where?

最佳答案

你尝试过吗:

@Data
@MappedSuperclass
public abstract class GeneralAuthor

@Entity
@DynamicInsert
public class Author extends GeneralAuthor {}

@Entity
@DynamicInsert
@Where(clause = "isDeleted=false")
public class ActiveAuthor extends GeneralAuthor {}

这里GeneralAuthor是一个基本数据结构(从当前Author复制粘贴它)。

Author 重命名为 ActiveAuthor,提供有关其含义的更多信息: Activity (未删除)作者记录。

新的实体Author实体允许您访问任何类型的作者(已删除或未删除),并且应该在映射到Book时使用。

这将允许您重用所有数据结构,并对同一作者实体拥有灵活的 View 。请注意:AuthorActiveAuthor 不声明任何自己的字段。

关于java - 如何忽略@Where hibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62194564/

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