简体   繁体   中英

Left join fetch and prefetching

I have a question , when we have a query like Select DISTINCT A from A a left join fetch ab 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 ab 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.

这可能是DISTINCT子句的问题,因为这里您具有A-> B之类的映射,所以在结果集中,您可能会有多个A代表B。使用Set集合填充数据。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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