Reputation: 1
i use querydsl, hibernate i want select data by Dto in Dto list but not working
here is my code
@Data
@Entity
public class Team {
@Id
@GeneratedValue
private Long id;
private String name;
@OneToMany(mappedBy = "team")
private List<Member> members = new ArrayList<>();
}
@Entity
@Setter
public class Member {
@Id
@GeneratedValue
private Long id;
private String name;
@ManyToOne
@JoinColumn(name = "team_id")
private Team team;
}
@Setter
public class TeamDto {
private Long id;
private String name;
private List<MemberDto> members = new ArrayList<>();
}
@Setter
public class MemberDto {
private Long id;
private String name;
}
test
@BeforeEach
void setup() {
queryFactory = new JPAQueryFactory(em);
Team team = new Team();
team.setName("teamA");
em.persist(team);
Member member = new Member("memberA");
member.setTeam(team);
em.persist(member);
Member member2 = new Member("memberB");
member2.setTeam(team);
em.persist(member2);
em.flush();
em.clear();
}
@Test
void t1() {
TeamDto teamDto = queryFactory
.select(Projections.fields(
TeamDto.class,
team.id,
team.name,
Projections.fields(
MemberDto.class,
member.id,
member.name
).as("members")
))
.from(team)
.fetchOne();
System.out.println("teamDto = " + teamDto);
}
error log is = java.lang.IllegalArgumentException: com.blog.querydsltest.domain.dto.MemberDto is not compatible with java.util.List
what is problem?? is impossible bring data by List dto?? i try to change Projections.fields to bean, construct, ... but not working how can i do ?
Upvotes: 0
Views: 1534
Reputation: 3913
Multi level aggregations are currently not supported by QueryDSL. There are also no concrete plans to support it as of now.
For a DTO solution that can fetch associations with it, I recommend you to have a look at Blaze-Persistence Entity Views. With Entity Views the code for your DTO would look something like the following:
@EntityView(Team.class)
public interface TeamDto {
@IdMapping public Long getId();
@Mapping("name") public String getName();
@Mapping("members") public List<MemberDTO> getMembers();
}
If members
is not an association on your TeamEntity
, you can map it through a @MappingCorrelated
binding.
Disclaimer: I am a contributor for Hibernate, QueryDSL and Blaze-Persistence.
Upvotes: 1