在 Spring Boot 2 JPA 中,我有以下两个多对多实体。
1- 劳动力:
@Entity
public class Labor {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(length = 100, nullable = false)
private String name;
@Column(length = 50)
private String mobile;
private Date dateOfBirth;
private boolean male;
private boolean active;
private String brife;
@Column(length = 500)
private String specifcation;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "labor_tag",
joinColumns = @JoinColumn(name = "labor_id"),
inverseJoinColumns = @JoinColumn(name = "tag_id"))
private Set<Tag> tags = new HashSet<>();
}
和标签表:
@Entity
public class Tag {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(length = 100, unique = true)
private String name;
private boolean active = true;
@ManyToMany(cascade = CascadeType.ALL, mappedBy = "tags")
@JsonIgnore
private Set<Labor> labors = new HashSet<>();
}
然后我定义了Labor Repository 来查询具有特定标签ID、性别或年龄的Labor
@Repository
public interface LaborReoistory extends JpaRepository<Labor, Long> {
@Query("select l from Labor l join l.tags t where (:tid is null or t.id in :tid) and " +
"(:isMale is null or :isMale = TRUE) and " +
"((:startYear is null or :endYear is null or :startYear > :endYear) or year(l.dateOfBirth) >= :startYear and year(l.dateOfBirth) <= :endYear)")
Page<Labor> findLaborsByCondition(@Param("tid") final long[] tid,
@Param("isMale") final Boolean isMale,
@Param("startYear") final Integer startYear,
@Param("endYear") final Integer endYear,
final Pageable pageable);
}
当我在控制器中使用此存储库时,我发现 Pagable 的totalElements属性返回计数到labour_tag中的记录(在本例中为 16 条记录),但我真正想要的是在给定条件下让totalElements 计数。JPA Pagable 是否支持此类查询或如何找到解决方法?
谢谢