JDBC and "lazy" loading
843859Apr 30 2006 — edited Apr 30 2006For a school project we have to create a Swing based application that works directly with the database. While we were looking at how to map our classes to the database we came across Hibernate. Unfortunately after a week or so we came to the conclusion that Hibernate in combination with a Swing app isn't very simple to work out, especially because we have so little experience and time for the project is running out. The main problem is ofcourse the fact that lazy loading won't just work when creating a Swing application this way. So now we have to finish the job with plain old JDBC and a bunch of DAO classes. But I have 2 big questions on how to handle this with JDBC and I hope some of you guys can help me with these.
As an example I'll use a class "A", and every object of this class holds an ArrayList of objects of class "B".
1) What would be the best solution to simulate "lazy loading" with JDBC? Currently we load an object like this:
step 1) we load the data from the database and put it in an object of class "A"
step 2) for every "A" we do a rowcount on the table that holds data for "B" and count the rows that are related to this "A" object
step 3) when the rowcount is 0 we set a boolean in "A" to true, which means "A" doesn't have to load its collection anymore (because the list is empty). Otherwise we put this boolean to false which means that when it has to access the list of "B" objects "A" knows it has to load the list from the database first.
Is this a good way of working? Or should we do this on a different way? Or maybe with less SQL statements as we have to perform a rowcount for every object?
2) Ofcourse we won't update the database after every change in the model. The problem I'm having with this is how we would keep track of the objects in the list that we want to delete from the database? Should we keep in class "A" a second list of objects from "B" that will hold all objects that must be deleted next time we update the database? Or can we do this on another way?
I hope someone can help me with this or at least help me in the right direction. Thanks in advance ;)