Left join fetch and prefetching
I have a question , when we have a query like Select DISTINCT A from A a left join fetch a.b JPA will bring the object A populated, and the object b, which is a oneToMany relationship with a, will be prefetched. What exactly means being prefetched, because when i debug this object it has null values, and there is data in the database, so i am assuming that it is a proxy object. Does the proxy contain any data that 开发者_开发百科was prefetched from the database, and it only gets populated on a.b when i actually call a method like a.getB().size()?
I thought that if i used the "left join fetch" i wouldn't need to call a.getB().size(), but if i don't do this, the object b will remain as a proxy,and then in the presentation layer if any object call any attribute of b, there is a nullpointer.
I might add as well that this b object is mapped as being lazily loaded, that's why i am using the fetch afterall. :) Also, even if i am using "left join fetch" and call a.getB().size(), it would not do any aditional queries right? the prefetching would be responsible for getting all of b's data,... it only needs me to call a method so it transfers the data from the proxy to the actual object?
thanks in advance for any explanation, this is really bothering me...
This depends on your JPA implementation.
As far as I know, at least with EclipseLink, if you use a join fetch, the attribute will always be instantiated, never null.
This might be the issue of DISTINCT clause, Because here u have mapping like A->B, So in the result set you might have multiple results of A for B. Use Set collection to populate the data.
精彩评论