具有投影的 Hibernate 标准不执行 @OneToMany 映射的查询

发布于 2024-10-11 15:43:36 字数 1305 浏览 7 评论 0原文

我有一个域对象 Expense,它有一个名为 initialFields 的字段。

它的注释如下:

@OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.ALL }, orphanRemoval = true)
@JoinTable(blah blah)
private final List<Field> initialFields;

现在我尝试使用投影来出于性能原因仅提取某些字段,但这样做时,initialFields 字段始终为空。这是唯一的 OneToMany 字段,也是我尝试使用具有这种行为方式的投影检索的唯一字段。如果我使用常规 HQL 查询,initialFields 会被适当填充,但当然我不能限制字段。

部分投影代码:

Criteria criteria = session.createCriteria(Payment.class);
criteria.createAlias("expense", "e");

ProjectionList properties = Projections.projectionList();
//Some restrictions and more fields
properties.add(Projections.property("e.initialFields"), "initialFields");
criteria.setProjection(properties);
criteria.setFetchMode("e.initialFields", FetchMode.JOIN);
criteria.setReadOnly(true);
criteria.setResultTransformer(Transformers.aliasToBean(Expense.class));

return criteria.list();

当我打开调试并打开显示 sql 时,提取初始字段的查询似乎没有创建/运行。有人见过这样的事情吗?

我刚刚尝试使用 HQL 投影,通过指定我想要提取的每个字段,然后手动构建对象。在这种情况下,Hibernate 构建的 SQL 对于initialFields 字段不正确。 expense1_.name 为 col_1_0_, .作为 col_2_0_,费用1_.account_id 作为 col_3_0_。 <代码>.因为 col_2_0_ 是提取初始字段的位置。我的猜测应该是 expense1_.id as col_2_0_

编辑:我刚刚删除了结果转换器来检查每个属性,并且initialFields 属性确实为空。

I have a domain object, Expense, that has a field called initialFields.

It's annotated as so:

@OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.ALL }, orphanRemoval = true)
@JoinTable(blah blah)
private final List<Field> initialFields;

Now I'm trying to use Projections in order to only pull certain fields for performance reasons, but when doing so the initialFields field is always null. It's the only OneToMany field and the only field I am trying to retrieve with the projection that is behaving this way. If I use a regular HQL query initialFields is populated appropriately, but of course I can't limit the fields.

Partial projection code:

Criteria criteria = session.createCriteria(Payment.class);
criteria.createAlias("expense", "e");

ProjectionList properties = Projections.projectionList();
//Some restrictions and more fields
properties.add(Projections.property("e.initialFields"), "initialFields");
criteria.setProjection(properties);
criteria.setFetchMode("e.initialFields", FetchMode.JOIN);
criteria.setReadOnly(true);
criteria.setResultTransformer(Transformers.aliasToBean(Expense.class));

return criteria.list();

When I turn on debugging and turn on show sql, the query to pull the initialFields doesn't appear to be created/run. Anyone ever seen anything like this?

I've just tried using HQL projection, by specifying each field I want to pull and then manually building the object. In this case the SQL built by Hibernate is incorrect for the initialFields field. expense1_.name as col_1_0_, . as col_2_0_, expense1_.account_id as col_3_0_. The . as col_2_0_ is where the initialFields would be pulled. My guess is that it should be expense1_.id as col_2_0_.

Edit: I just removed the Result Transformer to inspect each property and the initialFields property is indeed null.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世态炎凉 2024-10-18 15:43:36

我遇到了类似的问题,对我来说,正在标准中明确指定 joinType

Criteria criteria = session.createCriteria(Payment.class);
criteria.createAlias("expense", "e", CriteriaSpecification.LEFT_JOIN);

I had similar problem and for me was working explicitly specifying joinType in criteria.

Criteria criteria = session.createCriteria(Payment.class);
criteria.createAlias("expense", "e", CriteriaSpecification.LEFT_JOIN);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文