I have a large amount of rows in the database from which I need to create an XML document. I am using hibernate 3. The basic list() method in Criteria and Query interfaces looks dangerous: I quess it pretty much has to read all the records into memory even if I only iterate over them. Or is there some lazy loading magic? If not, I seem to have two options left: using scroll() or iterate() from Query (scroll is also present in Criteria). iterate doesn't look all that great either if I want to have minimal SQL roundtrips: "The first SQL query returns identifiers only". So am I right, do I have to use scroll() for this?
You can easily avoid that, when you tell Hibernate to initialize the required association. There are several different ways to do that. The easiest one is to add a JOIN FETCH statement to your FROM clause. I’m sure you’re not surprised when I tell you that selecting too many records slows down your application.
The problem with this is that once it runs this query, it will return multiple rows if the User it finds has logged into the system more than once. Again, it does this because it's an optional (left) join. If you were to take a look at the query built by Hibernate, it could look something like this (assuming the username being passed in is tpage ):
Hibernate now executes only 1 query instead of multiple ones and it also changed the SELECT clause of the query to include all columns mapped by the Book entity. 19:21:12,409 INFO [org.thoughts.on.java.model.TestJoinFetch] - Thorben Janssen wrote 1 books.
When you use FetchType.LAZY for all of your associations to avoid mistake 1 and 2, you will find several n+1 select issues in your code. This problem occurs when Hibernate performs 1 query to select n entities and then has to perform an additional query for each of them to initialize a lazily fetched association.
Try using scroll() in conjunction with this:
http://docs.jboss.org/hibernate/core/3.3/api/org/hibernate/StatelessSession.html
A command-oriented API for performing bulk operations against a database.
A stateless session does not implement a first-level cache nor interact with any second-level cache, nor does it implement transactional write-behind or automatic dirty checking, nor do operations cascade to associated instances. Collections are ignored by a stateless session. Operations performed via a stateless session bypass Hibernate's event model and interceptors. Stateless sessions are vulnerable to data aliasing effects, due to the lack of a first-level cache.
For certain kinds of transactions, a stateless session may perform slightly faster than a stateful session.
Use the setMaxResults() method on Criteria.
Criteria crit = sess.createCriteria(Cat.class);
crit.setMaxResults(maxResults);
crit.setFirstResult(firstResultIndex);
List cats = crit.list();
http://hibernate.org/hib_docs/v3/reference/en/html/querycriteria.html
http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Criteria.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With