admin管理员组文章数量:1123873
I have the following entities in my database:
@Getter
@Entity
class TopLevel {
@Column
String name;
@OneToMany
List<SubTypeOne> subtypes;
}
@Getter
@Entity
class SubTypeOne {
@ManyToOne()
SubTypeTwo property
}
@Getter
@Entity
class SubTypeTwo {
@Column
int finalProperty
}
I have a DTO that looks like this:
record TopLevelDto(String name, Collection<Integer> subTypeTwoProperties) {}
I'd like to create a DTO object directly from within a JPA query using JPQL. Right now, I can build the required objects efficiently using the following code:
String query = """
SELECT t
FROM TopLevel t
LEFT JOIN FETCH t.subtypes st
LEFT JOIN FETCH st.property
"""
// code to get TopLevel Object from query omitted
TopLevel t = ??
List<Integer> subTypeTwoProperties = t.getSubtypes().stream()
.map(s -> s.getProperty().getFinalProperty())
.toList();
TopLevelDto dto = new TopLevelDto(t.getName(), subTypeTwoProperties);
Is there way to do all of this just within the JPQL query itself?
I would expect a function or some other mechanism to convert the st
object in the query into a list. For example, I'm calling it collect_list
here:
String query = """
SELECT new TopLevelDto(t.name, collect_list(st.property))
FROM TopLevel t
LEFT JOIN FETCH t.subtypes st
LEFT JOIN FETCH st.property
"""
Does something like this, or an alternative, exist?
本文标签: javaIs there a way to create lists of joined objects in JPA queriesStack Overflow
版权声明:本文标题:java - Is there a way to create lists of joined objects in JPA queries? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736600294a1945208.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论