java - What is the occasion to use Eager Fetch in JPA? -
I think this is between eager fitch and lazy fitch Very difficult to choose . As we all know that after the session is completed, the eager joy can preserve the data, and the data on the more demand like the lazy fatch and the transaction ends It has been ended, it seems that eager fun can spend more resources and sometimes retrieve unnecessary data. For example, if there are 100 orders in the shopping cart and many "items" in each order are a two-level relationship), and we set these relationships to curious , even so That only want to get a specific shopping cart attribute:
// will run many questions for the timestamp // the end of the JPA / if all currencies set in ShoppingCart to bring curiosity Do ... date expiration = ShoppingCart.f Ind (id) .getExpireTimestamp (); It is still about to draw all the orders and all items (through additional questions), which is a huge waste.
However, on the other hand, code> lazy fetch can not be fast, because lazy fetch only requires a transaction to run a code. .
So my question is, get curious and between the classic fetch and between memory cost ( keen funk ) and database transaction cost (from sluggish fetch ), which is the winner?
Eager leaking is one if you load entities through EntityMangager, then use a JOIN is done. If you load them through the JPQL / Criteria API, then the secondary selection is employed.
Setting all organizations as LAZY, and using JoyFile instead, it is best to have a type-time liability strategy and it should never be sent to entity mapping.
A global Fake Plan is a bad idea, especially when it does not fit into all the app query scenarios.
So, you can use LAZY safely with query-time JOIN FETCH loading.
Comments
Post a Comment