gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 02:05:29 25 4
gpt4 key购买 nike

考虑一个采用构建器模式的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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com