简体   繁体   English

左联接获取可为空的孙子关联会导致NPE处于休眠状态

[英]left join fetch of nullable grandchild association causes NPE in hibernate

I have an entity called Foo. 我有一个名为Foo的实体。 It has a nullable many-to-one association to Bar. 它与Bar有一个可为空的多对一关联。 Bar has a non-nullable many-to-one association to Baz. Bar与Baz具有不可空的多对一关联。

My goal is to grab all Foo entities and eagerly fetch their Bar associations and, for those foo where foo.bar is non-null, eagerly fetch foo.bar.baz. 我的目标是获取所有Foo实体并热切获取其Bar关联,对于foo.bar不为null的foo,则热切获取foo.bar.baz。

Is this possible? 这可能吗? The following both cause hibernate to throw a NullPointerException inside its query engine: 以下两者都会导致休眠状态在其查询引擎内引发NullPointerException:

select f from Foo f left join fetch f.bar left join fetch f.bar.baz

select f from Foo f left join fetch f.bar.baz

Now, this works: 现在,这有效:

select f from Foo left join fetch f.bar

But that doesn't eagerly fetch f.bar.baz for those f with non-null bar. 但这并不急于为那些非空bar的f获取f.bar.baz。

You need to alias bar in your query: 您需要在查询中使用别名bar

select f from Foo f
  left join fetch f.bar b
  left join fetch b.baz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM