JPQL how to JOIN FETCH a List with anymatch condition against the List

Given Entities:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Entity
@Data
public class Collection {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String createdBy;
@OneToMany(mappedBy = "collection")
private List<CollectionAccess> collectionAccesses;
}
@Entity
@Data
public class CollectionAccess {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(
foreignKey =
@ForeignKey(
value = ConstraintMode.PROVIDER_DEFAULT,
name = "fk_collectionaccess_collection_collections_id"))
private Collection collection;
private Integer accessType;
private LocalDateTime expirationAtUtc;
}
</code>
<code>@Entity @Data public class Collection { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String createdBy; @OneToMany(mappedBy = "collection") private List<CollectionAccess> collectionAccesses; } @Entity @Data public class CollectionAccess { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @ManyToOne @JoinColumn( foreignKey = @ForeignKey( value = ConstraintMode.PROVIDER_DEFAULT, name = "fk_collectionaccess_collection_collections_id")) private Collection collection; private Integer accessType; private LocalDateTime expirationAtUtc; } </code>
@Entity
@Data
public class Collection {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  private String createdBy;

  @OneToMany(mappedBy = "collection")
  private List<CollectionAccess> collectionAccesses;

}

@Entity
@Data
public class CollectionAccess {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @ManyToOne
  @JoinColumn(
      foreignKey =
          @ForeignKey(
              value = ConstraintMode.PROVIDER_DEFAULT,
              name = "fk_collectionaccess_collection_collections_id"))
  private Collection collection;

  private Integer accessType;
  private LocalDateTime expirationAtUtc;
}

Given database:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>select id, created_by from collection;
+----+------------+
| id | created_by |
+----+------------+
| 40 | ABC123 |
+----+------------+
select * from collection_access;
+----+-------------+----------------------------+---------------+
| id | access_type | expiration_at_utc | collection_id |
+----+-------------+----------------------------+---------------+
| 2 | 0 | 2011-12-03 03:15:30.000000 | 40 |
| 3 | 1 | 2011-12-03 03:15:30.000000 | 40 |
+----+-------------+----------------------------+---------------+
</code>
<code>select id, created_by from collection; +----+------------+ | id | created_by | +----+------------+ | 40 | ABC123 | +----+------------+ select * from collection_access; +----+-------------+----------------------------+---------------+ | id | access_type | expiration_at_utc | collection_id | +----+-------------+----------------------------+---------------+ | 2 | 0 | 2011-12-03 03:15:30.000000 | 40 | | 3 | 1 | 2011-12-03 03:15:30.000000 | 40 | +----+-------------+----------------------------+---------------+ </code>
select id, created_by from collection;
+----+------------+
| id | created_by |
+----+------------+
| 40 |   ABC123   |
+----+------------+

select * from collection_access;
+----+-------------+----------------------------+---------------+
| id | access_type | expiration_at_utc          | collection_id |
+----+-------------+----------------------------+---------------+
|  2 |           0 | 2011-12-03 03:15:30.000000 |            40 |
|  3 |           1 | 2011-12-03 03:15:30.000000 |            40 |
+----+-------------+----------------------------+---------------+

As you guys can see, the collection 40 has 2 collection_access 2 and 3.

I want to do JPQL select a Collection that has id value is 40 and join fetch its CollectionAccess with a condition: if any CollectionAccess has accessType = 1 AND expirationAtUtc <= now (JOIN FETCH prevents N + 1 queries issue):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>createQuery(
"SELECT c FROM Collection c "
+ "LEFT JOIN FETCH c.collectionAccesses ca WHERE c.id = ?1 "
+ "AND (c.createdBy = ?2 OR (ca.accessType = ?3 AND ca.expirationAtUtc <= ?4))",
Collection.class)
.setParameter(1, id)
.setParameter(2, "DUMMY DATA")
.setParameter(3, 1)
.setParameter(4, now)
getSingleResult();
</code>
<code>createQuery( "SELECT c FROM Collection c " + "LEFT JOIN FETCH c.collectionAccesses ca WHERE c.id = ?1 " + "AND (c.createdBy = ?2 OR (ca.accessType = ?3 AND ca.expirationAtUtc <= ?4))", Collection.class) .setParameter(1, id) .setParameter(2, "DUMMY DATA") .setParameter(3, 1) .setParameter(4, now) getSingleResult(); </code>
createQuery(
      "SELECT c FROM Collection c "
          + "LEFT JOIN FETCH c.collectionAccesses ca WHERE c.id = ?1 "
          + "AND (c.createdBy = ?2 OR (ca.accessType = ?3 AND ca.expirationAtUtc <= ?4))",
      Collection.class)
  .setParameter(1, id)
  .setParameter(2, "DUMMY DATA")
  .setParameter(3, 1)
  .setParameter(4, now)
  getSingleResult();

I expect that after executing this query, I will get a Collection have id 40 and its TWO CollectionAccess, but it give me ONLY ONE CollectionAccess which has id 3. PLEASE NOTE THAT I SET A DUMMY/WRONG DATA TO ?2

I have done some research and found this Stackoverflow topic (main idea is we will have both JOIN and JOIN FETCH).

Based on that, I changed the query to:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>createQuery("SELECT c FROM Collection c LEFT JOIN c.collectionAccesses ca "
+ "LEFT JOIN FETCH c.collectionAccesses WHERE c.id = ?1 "
+ "AND (c.createdBy = ?2 OR (ca.accessType = ?3 AND ca.expirationAtUtc <= ?4))",
Collection.class)
.setParameter(1, id)
.setParameter(2, "ABC123") //NOW I SET CORRECT DATA TO ?2
.setParameter(3, 1)
.setParameter(4, now)
getSingleResult();
</code>
<code>createQuery("SELECT c FROM Collection c LEFT JOIN c.collectionAccesses ca " + "LEFT JOIN FETCH c.collectionAccesses WHERE c.id = ?1 " + "AND (c.createdBy = ?2 OR (ca.accessType = ?3 AND ca.expirationAtUtc <= ?4))", Collection.class) .setParameter(1, id) .setParameter(2, "ABC123") //NOW I SET CORRECT DATA TO ?2 .setParameter(3, 1) .setParameter(4, now) getSingleResult(); </code>
createQuery("SELECT c FROM Collection c LEFT JOIN c.collectionAccesses ca "
          + "LEFT JOIN FETCH c.collectionAccesses WHERE c.id = ?1 "
          + "AND (c.createdBy = ?2 OR (ca.accessType = ?3 AND ca.expirationAtUtc <= ?4))",
      Collection.class)
  .setParameter(1, id)
  .setParameter(2, "ABC123")  //NOW I SET CORRECT DATA TO ?2
  .setParameter(3, 1)
  .setParameter(4, now)
  getSingleResult();

Now it return to me DUPLICATED

Then I decided to write nesting select and hope that it will work:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>createQuery(
"SELECT t FROM (SELECT c FROM Collection c "
+ "LEFT JOIN c.collectionAccesses ca WHERE c.id = ?1 "
+ "AND (c.createdBy = ?2 OR (ca.accessType = ?3 AND ca.expirationAtUtc <= ?4))) t "
+ "JOIN FETCH t.collectionAccesses ",
Collection.class)
.setParameter(1, id)
.setParameter(2, "ABC123")
.setParameter(3, 1)
.setParameter(4, now)
getSingleResult();
</code>
<code>createQuery( "SELECT t FROM (SELECT c FROM Collection c " + "LEFT JOIN c.collectionAccesses ca WHERE c.id = ?1 " + "AND (c.createdBy = ?2 OR (ca.accessType = ?3 AND ca.expirationAtUtc <= ?4))) t " + "JOIN FETCH t.collectionAccesses ", Collection.class) .setParameter(1, id) .setParameter(2, "ABC123") .setParameter(3, 1) .setParameter(4, now) getSingleResult(); </code>
createQuery(
      "SELECT t FROM (SELECT c FROM Collection c "
              + "LEFT JOIN c.collectionAccesses ca WHERE c.id = ?1 "
              + "AND (c.createdBy = ?2 OR (ca.accessType = ?3 AND ca.expirationAtUtc <= ?4))) t "
              + "JOIN FETCH t.collectionAccesses ",
      Collection.class)
  .setParameter(1, id)
  .setParameter(2, "ABC123")
  .setParameter(3, 1)
  .setParameter(4, now)
  getSingleResult();

BUT NO LUCK:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>java.lang.IllegalArgumentException: org.hibernate.query.SemanticException: Select item at position 1 in select list has no alias (aliases are required in CTEs and in subqueries occurring in from clause)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:143) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
</code>
<code>java.lang.IllegalArgumentException: org.hibernate.query.SemanticException: Select item at position 1 in select list has no alias (aliases are required in CTEs and in subqueries occurring in from clause) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:143) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final] </code>
java.lang.IllegalArgumentException: org.hibernate.query.SemanticException: Select item at position 1 in select list has no alias (aliases are required in CTEs and in subqueries occurring in from clause)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:143) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]

How can i archive this goal?

I want to do JPQL select a Collection that has id value is 40 and join fetch its CollectionAccess with a condition: if any CollectionAccess has accessType = 1 AND expirationAtUtc <= now.

I expect that after executing this query, I will get a Collection have id 40 and its TWO CollectionAccess

Please help, thank you so much

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật