I have a contact and address entities where contact has 1-to-many relationship with address
I am trying to fetch all the addresses for all contacts in minimum sqls. I read the chapter 13(optimizing fetching and caching) of Java persistence with hibernate. It specifies 3 possible solutions:
1. Using batch-size
2. Using fetch="subselect"
3. Using fetch = "join"
Below is my java code:
List contacts = session.createQuery("from roseindia.tutorial.hibernate.Contact").list();
System.out.println("contacts.size" + contacts.size());
Iterator it = contacts.iterator();
Contact contact = (Contact)it.next();
1. Batch size works fine and gives me 1 sql for the association table when i increase the batch size sufficiently
2. fetch="subselect" and fetch="join" doesn't work as expected and it gives me N+1 sqls for the above code
I read at below url that fetch="subselect" and fetch="join" doesn't work for HQLs and criteria. It works only with load/get
I got answer to 1 question. On reading more, I found that fetch="subselect" only applies to collection and not entity proxies.
Following is quote from book "subselect fetching is available only for collections and not entity proxies"
I interpret from above that subselect is available only for collections containing value types and not entity types.
I still am looking for answer to the 2nd question(fetch="join")
expectation is the root of all heartache - shakespeare. tiny ad: