有没有办法从JPQL中的选择语句内部的内部类构建对象?
我正在尝试从jpql中的选择语句中的内部类构建对象。
我正在从实体中提取数据并使用它们来构建DTO。类似的事情:
SELECT new project.models.dtos.RequestDTO(r.id,
t.id,
t.description,
...)
FROM Request r
JOIN Topic t. etc.
现在,此requestdto具有一些基于原始实体的内部
public class RequestDTO{
private Long id;
private TopicDTO topic;
public class TopicDTO{
private Long id;
private String description;
}
public RequestDTO(Long id, TopicDTO topic, ...){
this.id = id;
this.topic = topic
}
}
类主要查询,就像这样:
SELECT new project.models.dtos.RequestDTO(
r.id,
new project.models.dtos.RequestDTO.TopicDTO(
t.id,
t.description),
r.stuff,
...)
FROM Request r
JOIN Topic t
etc.
有办法这样做吗?提前致谢!
I'm trying to build an object from an inner class inside a SELECT statement in JPQL.
I'm extracting data from an entity and using them to build a DTO. Something like this:
SELECT new project.models.dtos.RequestDTO(r.id,
t.id,
t.description,
...)
FROM Request r
JOIN Topic t. etc.
Now, this RequestDTO has some inner classes, that are based on the original entity, and they are used in the constructor, like this:
public class RequestDTO{
private Long id;
private TopicDTO topic;
public class TopicDTO{
private Long id;
private String description;
}
public RequestDTO(Long id, TopicDTO topic, ...){
this.id = id;
this.topic = topic
}
}
What i would like to do is build the inner objects DIRECTLY inside of the select statement of the main query, like so:
SELECT new project.models.dtos.RequestDTO(
r.id,
new project.models.dtos.RequestDTO.TopicDTO(
t.id,
t.description),
r.stuff,
...)
FROM Request r
JOIN Topic t
etc.
Is there a way to do so? Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是不可能的。解决方法的一个选项是创建一个
requestdto
的构造函数,该构建器还通过某些参数构建topicdto
:I don't think that's possible. One option to workaround this would be to create a constructor of
RequestDTO
that also buildsTopicDTO
by some parameters: