我发现需要通过子类中的属性来限制子集合的大小。
在按照这个指南后,我有以下结果:
@FilterDef(name="dateFilter", parameters=@ParamDef( name="fromDate", type="date" ) )
public class SystemNode implements Serializable {
@Getter
@Setter
@Builder.Default
// "startTime" is a property in HealthHistory
@Filter(name = "dateFilter", condition = "startTime >= :fromDate")
@OneToMany(mappedBy = "system", targetEntity = HealthHistory.class, fetch = FetchType.LAZY)
private Set<HealthHistory> healthHistory = new HashSet<HealthHistory>();
public void addHealthHistory(HealthHistory health) {
this.healthHistory.add(health);
health.setSystem(this);
}
}
然而,当使用Spring Data JPA时,我并不真正理解如何切换此过滤器。我像这样获取我的父实体:
public SystemNode getSystem(UUID uuid) {
return systemRepository.findByUuid(uuid)
.orElseThrow(() -> new EntityNotFoundException("Could not find system with id " + uuid));
}
然后这个方法会调用被Spring支持的仓库接口:
public interface SystemRepository extends CrudRepository<SystemNode, UUID> {
Optional<SystemNode> findByUuid(UUID uuid);
}
如何让这个过滤器与Spring良好协作?我希望能够在需要时以编程方式激活它,而不是全局激活。有些情况下,忽略过滤器是可行的。
我正在使用Spring Boot 1.3.5.RELEASE
,目前无法更新。
EntityManager
。使用EntityManager.unwrap(Session.class)
获取底层的Hibernate Session。调用enableFilter
,调用存储库方法,调用disableFilter
。 - M. Deinum