Java Reference
In-Depth Information
Using JOIN in queries
JPQL doesn't allow you to specify fields of an object to be retrieved. Take the following
JPQL query:
Query q = em . createQuery ( "SELECT s FROM StockPriceImpl s" );
That query will always yield this SQL statement:
SELECT <enumerated list of non-LAZY fields> FROM StockPriceTable
If you want to retrieve fewer fields in the generated SQL, you have no option but to mark
them as lazy. Similarly, for fields that are marked as lazy, there is no real option for fetching
them in a query.
If there are relationships between entities, the entities can be explicitly joined in a query in
JPQL, which will retrieve the initial entities and their related entities in one shot. For ex-
ample, in the stock entities, this query can be issued:
Query q = em . createQuery ( "SELECT s FROM StockOptionImpl s " +
"JOIN FETCH s.optionsPrices" );
That results in an SQL statement similar to:
SELECT t1.<fields>, t0.<fields> FROM StockOptionPrice t0, StockPrice t1
WHERE ((t0.SYMBOL = t1.SYMBOL) AND (t0.PRICEDATE = t1.PRICEDATE))
Search WWH ::




Custom Search