小编pro*_*ook的帖子

HQL和内部类(例如构建器)

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

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

select new Result.Builder(f, b) from Foo f, Bar b where ...
Run Code Online (Sandbox Code Playgroud)

但是,我最终得到了错误

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

一种解决方案是将Builder移动到一个单独的类,但我喜欢使用它的实体整齐地包装的Builder.

有没有办法,一种语法让Hibernate识别select子句中的内部类?

java hibernate

4
推荐指数
1
解决办法
568
查看次数

标签 统计

hibernate ×1

java ×1