HQL和内部类(例如构建器) [英] HQL and inner classes (such as builders)

查看:38
本文介绍了HQL和内部类(例如构建器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑采用构建器模式的 Result DTO:

Consider a Result DTO employing a builder pattern:

package com.example;
public class Result {
    int someValue;

    public static class Builder {
        private final Foo foo;
        private final Bar bar;

        public Builder(Foo foo, Bar bar) {
            this.foo = foo;
        }

        public Result build() {
            Result r = new Result();
            r.someValue = /* compute value based on supplied Foo and Bar */;
            return r;
        }
    }
}

现在,我想在HQL查询中创建构建器,例如:

Now, I want to create the builder in an HQL query, such as:

select new Result.Builder(f, b) from Foo f, Bar b where ...

但是,我最终会出错

无法找到类[com.example.Result.Builder]

Unable to locate class [com.example.Result.Builder]

一种解决方案是将Builder移至单独的类,但是我喜欢Builder与其实体整齐地包装.

One solution is to move the Builder to a separate class, but I like the Builder neatly packed with its entity.

有没有一种方法可以使Hibernate识别select子句中的内部类?

推荐答案

事实证明,我最终找到了解决方案.正确的语法是带有内部类的 $ 分隔符的完全限定名称,例如:

It turns out I found the solution in the end; The proper syntax is fully qualified name with a $ separator of inner class, such as:

select new com.example.Result$Builder(f, b) from ...

这篇关于HQL和内部类(例如构建器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆