【发布时间】:2026-02-08 10:55:01
【问题描述】:
考虑使用构建器模式的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 ...
但是,我最终得到了错误
找不到类 [com.example.Result.Builder]
一种解决方案是将 Builder 移到一个单独的类中,但我喜欢 Builder 与其实体整齐地打包在一起。
有没有办法让 Hibernate 识别 select 子句中的内部类?
【问题讨论】:
-
@ᴊᴀᴠʏ 哦...我的 google-fu 很弱。然而,这应该被记录在一个比 bugtracker 评论更好的地方。
-
从Documentation找到的,命名的持久类是一个接口是可以接受的。您可以使用
元素声明该接口的实现类。您可以持久化任何静态内部类。使用标准形式指定类名,例如 Foo$Bar.