【发布时间】:2022-01-12 12:45:04
【问题描述】:
我正在尝试为扩展我的基础存储库的所有存储库定义通用查询:
@NoRepositoryBean
public interface DocumentRepository<T extends BaseDocument> extends JpaRepository<T, Long> {
@Query(value = "FROM BaseDocument bd WHERE (bd.createdAt IS NULL OR :to IS NULL OR bd.createdAt < :to) AND (bd.deletedAt IS NULL OR :from IS NULL OR bd.deletedAt > :from)")
Iterable<T> findAllActiveBetween(OffsetDateTime from, OffsetDateTime to);
}
@MappedSuperclass
@Where(clause="deleted_at IS NULL")
abstract public class BaseDocument {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private OffsetDateTime createdAt;
private OffsetDateTime deletedAt;
}
有问题的部分是在 @Query 中使用 BaseDocument。 是否可以在所有子存储库中定义这样的方法而不重复?
【问题讨论】:
标签: spring-data-jpa spring-data