有没有办法从JPQL中的选择语句内部的内部类构建对象?

发布于 2025-02-12 02:34:10 字数 851 浏览 1 评论 0原文

我正在尝试从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤感在游骋 2025-02-19 02:34:10

我认为这是不可能的。解决方法的一个选项是创建一个requestdto的构造函数,该构建器还通过某些参数构建topicdto

public class RequestDTO{
    private Long id;
    private TopicDTO topic;

    public RequestDTO(Long id, Long topicId, String topicDescription, ...){
        this.id = id;
        this.topic = new TopicDto(topicId, topicDescription);
    }

    public class TopicDTO{
        private Long id;
        private String description;
        // Constructor, getter, setter
    }

}

I don't think that's possible. One option to workaround this would be to create a constructor of RequestDTO that also builds TopicDTO by some parameters:

public class RequestDTO{
    private Long id;
    private TopicDTO topic;

    public RequestDTO(Long id, Long topicId, String topicDescription, ...){
        this.id = id;
        this.topic = new TopicDto(topicId, topicDescription);
    }

    public class TopicDTO{
        private Long id;
        private String description;
        // Constructor, getter, setter
    }

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文