left join fetch of nullable grandchild association causes NPE in hibernate
I have an entity called Foo. It has a nullable many-to-one association to Bar. Bar has a non-nullable many-to-one association to 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.
Is this possible? The following both cause hibernate to throw a NullPointerException inside its query engine:
select f from Foo f left join fetch f.b开发者_高级运维ar 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.
You need to alias bar
in your query:
select f from Foo f
left join fetch f.bar b
left join fetch b.baz
精彩评论