作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
考虑一个采用构建器模式的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;
}
}
}
现在,我想在 HQL 查询中创建构建器,例如:
select new Result.Builder(f, b) from Foo f, Bar b where ...
但是,我最终还是报错了
Unable to locate class [com.example.Result.Builder]
一个解决方案是将 Builder 移到一个单独的类中,但我喜欢将 Builder 与其实体整齐地打包在一起。
有没有办法让 Hibernate 识别 select 子句中的内部类?
最佳答案
事实证明我最终找到了解决方案;正确的语法是带有内部类的$
分隔符的完全限定名称,例如:
select new com.example.Result$Builder(f, b) from ...
关于java - HQL 和内部类(例如构建器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35503917/
我是一名优秀的程序员,十分优秀!