Lazy loading
When you're working with relations between your classes Castor will
automatically load related data. In a data model with many relations this can
lead to a lot of unnecessary SQL SELECT's against the database. To postpone
loading data until you actually need it (when you fetch it from your Collection
with e.g. iterator.next()) you may use the "lazy loading" mechanism. It's
simple to use: add the attribute lazy="true" to the field with the
Collection, for example on the relation from Actor to Movie:
<field name="movies"
type="hansen.playground.Movie"
collection="collection"
lazy="true">
|
|
Let's run the test program again with lazy loading:
***Actor.setMovies: 2 movies to actor "Keanu Reeves"
Movies starring Keanu Reeves
***Media.addMovie: "The Matrix" to media "DVD"
***Movie.addActor: "Keanu Reeves" to movie "The Matrix"
***Actor.setMovies: 1 movies to actor "Laurence Fishburne"
***Movie.addActor: "Laurence Fishburne" to movie "The Matrix"
Movie: The Matrix
Media: DVD
Actor: Keanu Reeves
Actor: Laurence Fishburne
***Media.addMovie: "Chain Reaction" to media "VHS"
***Movie.addActor: "Keanu Reeves" to movie "Chain Reaction"
Movie: Chain Reaction
Media: VHS
Actor: Keanu Reeves
|
|
- Listing 4: Output
from ReadActor with lazy loading enabled -
That's quite a change. The following can be seen:
- Initially we only created the "Keanu Reeves" instance of Actor,
which apparently contained two Movie objects. But as we'll see shortly,
these are not yet fully built.
- The instant we use iterator to fetch a movie Castor will create all
the instances needed by the first movie: "The Matrix".
- When we use iterator for the second and last movie Castor will
create the last movie and actor instances.
If we also set lazy loading on the other two Collection fields (Movie
to Actor and Media to Movie) we get this output:
***Actor.setMovies: 2 movies to actor "Keanu Reeves"
Movies starring Keanu Reeves
***Media.setMovies: 1 movies to media "DVD"
***Movie.setActors: 2 actors to movie "The Matrix"
Movie: The Matrix
Media: DVD
Actor: Keanu Reeves
***Actor.setMovies: 1 movies to actor "Laurence Fishburne"
Actor: Laurence Fishburne
***Media.setMovies: 1 movies to media "VHS"
***Movie.setActors: 1 actors to movie "Chain Reaction"
Movie: Chain Reaction
Media: VHS
Actor: Keanu Reeves
|
|
- Listing 5: Output
from ReadActor with more lazy loading enabled -
This time it's even more obvious that the objects are created as late as
possible.
If you're working with a large database with many relations you will
certainly need lazy loading. When you use it you must ensure that you're working
inside a transaction (begin/commit) when the objects are lazy loaded. If not
you'll get a runtime error like this:
RuntimeException: Transaction is closed!
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|