当一个 table 中的计数等于另一个 table 中的列值时,单个 jpa @Query 到 return true/false
Single jpa @Query to return true/false when count in one table is equal to column value in other table
我有 2 个实体:具有一对多关系的 Leaflet 和 Page(每个 Leaflet 有多个页面)
@Entity
Leaflet {
@Id
@GeneratedValue
private UUID leafletId;
private Integer noPages;
@OneToMany(mappedBy = "leaflet", cascade = CascadeType.ALL, orphanRemoval = true)
Set<Page> pages = new HashSet<>();
}
@Entity
Page {
@Id
@GeneratedValue
private UUID pageId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "leaflet_id")
private Leaflet leaflet;
@Enumerated
private PageStatus status = PageStatus.CREATED;
}
和状态枚举
public enum PageStatus {
CREATED,
FRAMED
}
我想向 return 写入单个查询是否给定 Leaflet 的所有页面都已装裱。所以我写了这个
@Repository
public interface PageRepository extends JpaRepository<Page, UUID> {
@Query("SELECT case when (COUNT(p) = l.noPages) then true else false end from Page p inner join Leaflet l on p.leaflet.leafletId = l.leafletId where p.status = 1 and l.leafletId = ?1")
boolean allPagesFramed(UUID leafletId);
}
但是出现错误,这意味着我不能直接使用 l.noPages
ERROR: column "leaflet1_.no_pages" must appear in the GROUP BY clause or be used in an aggregate function
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
有没有办法让它成为 1 个查询?
当然,我可以首先 select l.noPages 第一次点击数据库,然后将这个值注入到上面的查询(而不是加入),我现在正在做的解决方法。
您可以根据页面 table 执行此操作。使用 nativeQuery = true
@Query(value = "select case when ( count(*) > 0 ) then false else true end " +
"from page p " +
"where p.leaflet_id = ?1 and p.status <> 1 ", nativeQuery = true)
boolean allPagesFramed(UUID leafletId);
如果页面至少有一个状态不同于 1 (FRAMED),则查询 return 错误,并非所有页面都是 FRAMED。
我有 2 个实体:具有一对多关系的 Leaflet 和 Page(每个 Leaflet 有多个页面)
@Entity
Leaflet {
@Id
@GeneratedValue
private UUID leafletId;
private Integer noPages;
@OneToMany(mappedBy = "leaflet", cascade = CascadeType.ALL, orphanRemoval = true)
Set<Page> pages = new HashSet<>();
}
@Entity
Page {
@Id
@GeneratedValue
private UUID pageId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "leaflet_id")
private Leaflet leaflet;
@Enumerated
private PageStatus status = PageStatus.CREATED;
}
和状态枚举
public enum PageStatus {
CREATED,
FRAMED
}
我想向 return 写入单个查询是否给定 Leaflet 的所有页面都已装裱。所以我写了这个
@Repository
public interface PageRepository extends JpaRepository<Page, UUID> {
@Query("SELECT case when (COUNT(p) = l.noPages) then true else false end from Page p inner join Leaflet l on p.leaflet.leafletId = l.leafletId where p.status = 1 and l.leafletId = ?1")
boolean allPagesFramed(UUID leafletId);
}
但是出现错误,这意味着我不能直接使用 l.noPages
ERROR: column "leaflet1_.no_pages" must appear in the GROUP BY clause or be used in an aggregate function org.hibernate.exception.SQLGrammarException: could not extract ResultSet
有没有办法让它成为 1 个查询? 当然,我可以首先 select l.noPages 第一次点击数据库,然后将这个值注入到上面的查询(而不是加入),我现在正在做的解决方法。
您可以根据页面 table 执行此操作。使用 nativeQuery = true
@Query(value = "select case when ( count(*) > 0 ) then false else true end " +
"from page p " +
"where p.leaflet_id = ?1 and p.status <> 1 ", nativeQuery = true)
boolean allPagesFramed(UUID leafletId);
如果页面至少有一个状态不同于 1 (FRAMED),则查询 return 错误,并非所有页面都是 FRAMED。