I'm trying to get up the learning curve with Hibernate. My primary goal right now is to get Hibernate to cache objects for me. I'm still pretty unsure of the distinction between the query cache and the second-level cache, but I assume that understanding will come with time and usage.
I've got all the mapping done for a Person object, and it retrieves fine. However, when I refresh the web page that retrieves, it calls loadAll(Person.class), and it goes back to the database every time. I can call createQuery("from Person").setCacheable(true), which does not go back to the database, so that's a help. Is that an appropriate thing to do, or is there a better way to get the objects out of cache instead. Basically, I want to load them once from the DB, and never load them again. I'm good with the assumption that no other programs are modifying the database tables in question.
However, even createQuery("from Person").setCacheable(true) does a complete refresh from the database whenever I change an instance of Person and call saveOrUpdate(). I'm not sure why it needs to, since my instance of the object is still authoritative. So the list of Persons is reloaded, which can be very expensive, especially if there are many dependencies.
How can I tell Hibernate that I'm wanting it to load the data once, and then never go back to the DB for this particular table, even if I update an instance and save the changes?
In the app I'm going to be writing, one area where I'm really looking for Hibernate to help me is where we have a Student class that has a large number of dependent instances and collections--there are a total of about 20 SELECTs involved, some of which are joing several tables. What I'd like is to load the Student record on an as-needed basis, but once I have it, always assume that the data I already loaded is valid, and never go back to the DB for any reason to refetch that particular student or his dependencies. I may update something and save changes back to the DB, but I don't want to reload anything if I do.
Can someone give me a push in the right direction?
_________________ Chris Collins
|