Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate in a DAO pattern & lazy loading

 
Olivier Croisier
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a small, non-web test application, I have 2 classes : Father and Child. Father is stored in the FATHER table, the children are in the CHILD one. And from the Java POV, the Father class contains a Set of Child(ren) and a getChildren() method to retrieve it.
Nothing complicated here

I use Hibernate to persist those data.

First test : I wrote a small "main" method in which I open a Session, a Tx, then add/remove/list some Fathers, and add/delete/list Children for a particular Father. The Tx and the Session are then closed, and it works OK.

Second test : I want to apply the DAO pattern. So I create a FatherDao that encapsulates all the DB-related stuff, in which I have a getById method.
Using that method from a new, simplified "main" method, I retrieve a particular Father. So far, so good.
PROBLEM : At this point, the DAO method that gave me the Father has closed the session. So when I try to call father.getChildren(), Hibernate tries to lazy-load the collection of Child(ren), and then dies a horrible death, complaining that I am not in a Session anymore.

QUESTION : I know that the "Session In View" pattern solves that in a webapp, but how can a strict DAO demarcation be achieved with Hibernate in non-web applications ?

I searched the official doc and many books, but they all focus on webapps and the "Session In View", and do not seem to care about traditional apps... Or is the answer so obvious that it does not deserve any explanation ?

Please help me !
 
Travis Hein
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your data set is small, like the amount of children for a given father,
you could turn off the default lazy behavior for the mapped tables,

for example, in Father.hbm.xml:
...
<hibernate-mapping default-lazy="false">
<class name="package.Father" table="father">
...
 
Olivier Croisier
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's what I did, and then it works fine. But that means I have to disable ALL lazy-loading capabilities, for ALL my mapped associations ! By doing that, I'd loose one of the most useful features of Hibernate...

There *must* be a proper answer to my problem, I cannot believe that all real-world, entreprise-class Hibernate-based applications have the lazy-loading feat disabled !

I have searched the net for tutorials, blog entries, articles, reference doc, but they were either dealing with a webapp, or weren't applying the DAO pattern.

I'd like to see a clear, simple example with only 2 mapped classes linked by a collection, with the DAO pattern applied !
 
Travis Hein
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm, i have example of a one to one mapping without lazy=false set, that is i don't have to disable lazy loading.

it uses springframework, hibernate 3, and although is running inside a web application, the database access code can be booted up outside of the webapp container.

the design i am using is
domain / manager - springframework manages transactions here with transaction proxy, also logical/functional grouping of things.

dao: invoke hibernate named queries, using the springframework HibernateDAOSupport.

dto: the POJO beans, and the hibernate xml mapping files (the hibernate session is loaded with springframework configuration.

in my instance, i have an "inventory item" which is a base table for materials, and equipment. then i have a "material" table, and an "equipment" table.

now, the material and equipment tables have an inventory_id column that is foreign key to primary key of the inventory_item table.

it is mapped in hibernate using



note that named queries with hibernate are a good thing, they get validated as the app boots up.

and then the mapping for the 1:1 relationship to the parent inventory table:



DTO beans: mostly getter / setters for bean pattern in a class



and the material. note the nested InventoryItem bean (from above).




the spring framework config for hibernate dto mapping xml:




so, now when i want to get me a material from the manager, which calls the dao, that runs the named query, and the result is hibernate populates the material DTO, containing the Inventory mapped bean.

Though it is not a collection like you were wanting an example on, this is the closest i have in my area, and hope it is a little useful to you.
 
ravi bhosale
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You can go for some patterns like Command pattern and DAO.

Implement your business logic inside Command beans execute method.

Implement Class, which will invoke execute command of your bean.

Now this call will first associate Hibernate Session with your Thread and then execute your business logic. After that it will close session object. Now in your business logic if you call multiple DAO, all will access same Hibernate session and when you access for lazy object, it will go to database with associated session.


Hope this will solve your problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic