Retrieve DTO with list inside using JPA

Solution for Retrieve DTO with list inside using JPA
is Given Below:

Is there a way to get such object within a single query in JPA?

data class PartcipantsDTO(
   val max: Int,
   val list: List<ParticipantCardDTO>
) // this one

   val id: Long,
   val name: String

Assuming that entities looks like:

   @Id id: Long,
   name: String,
   @ManyToOne event: EventEntity

   @Id id: Long,
   @OneToMany participants: List<Long>

To search by EventEntity id was trying query like:

SELECT new com.***.ParticipantsDTO(
   SELECT new ParticipantCardDTO(,
FROM ParticipantEntity p, EventEntity e
WHERE IN e.participants AND = :id

But it doesn’t work

I do know that it can be done with 2 separate queries, but I want to avoid opening 2 connections due to inefficiency

you can get ParticipantEntity using JPA, then map it and its participants list to DTO in Java, hibernate will handle its properties.

this is because hibernate have cache and handles performance issues itself.