Get chart structure in sleep mode

Object structure is like this

  • Invoice
    • Customer
    • the date
    • amount
    • has many products (product, quantity, price)
    • has many ServiceLines (service, quantity, price)
    • has many PaymentOptions (PaymentType (Check, Receipt, etc.), Date, Sum)

If I need to get a list of invoices for a certain period with Hibernate, it is very easy to do with lazy loading without writing code that just calls get ... BUT there is a drawback of too many database calls, so this solution is not in order in a multi-user environment.

Using simple JDBC, I solved this using 3 queries: 3 connections between invoices and products, invoices and services, as well as options and invoices. After that, I built the object in memory.

The same thing can be done with Hibernate, which I know. But my question is that there is no such thing as a load schedule, and therefore can I transfer the list of accounts and the minimum number of calls (optimal) for data extraction?

+5
source share
2 answers

You can use link retrieval queries to get the entire graph of an object in memory:

Query query = entityManager.createQuery("select distinct invoice from Invoice as invoice " 
            + "left join fetch invoice.productLines "                
            + "left join fetch invoice.serviceLines "
            + "left join fetch invoice.paymentOptions");
for (Object object : query.getResultList()) {
    // Code here
}
+4
source

You may like the batch selection for your lines of accounts and the desire to receive from several to one association, for example Customer.

, , -, .

JPQL/HQL ( Russ), , :

Query query = entityManager.createQuery("select distinct invoice from Invoice as invoice " 
            + "left join fetch invoice.productLines "                
            + "left join fetch invoice.serviceLines "
            + "left join fetch invoice.paymentOptions");
for (Object object : query.getResultList()) {
    // Code here
}

, , "" .

, JPQL, , , . , .

+4

All Articles