-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Sharing Session Factory Between Controller and Model Layer
PostPosted: Sun Mar 19, 2006 10:22 am 
Newbie

Joined: Wed Feb 22, 2006 1:24 am
Posts: 4
Location: Brazil
Dear Forum member,

When you create a TableNameHome.java using Hibernate tools a method called getSessionFactory() is created. This method returns the SessionFactory object using a JNDI approach.

Considering that I am using Struts, basically, there are two ways to create a SessionFactory object that will be available to all users on each call to the application: Using Servlet filters (http://www.hibernate.org/43.html) or creating a Hibernate Plug In (http://www.hibernate.org/105.html).
I am using the second approach: the plug in.

This solution does not use JNDI, but it creates a SessionFactory during Struts initialization and stores this object at the ServletContext.

The point here is: How to retrieve this object (SessionFactory) at a model layer class (the class TableNameHome.java).

The solution provided at http://www.hibernate.org/105.html shows a way to retrieve the SessionFactory object created by the plug in using:
servletContext.getAttribute(HibernatePlugIn.SESSION_FACTORY_KEY)

This can be easily done in a class derived from org.apache.struts.action.Action, which provides a method to retrieve the ServletContext, but how to perform this at the model layer?
To be more specific in the class created by Hibernate Tools, the class TableNameHome.java


Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 22, 2006 1:21 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Hi,

If i understand the hibernate struts plugin approach correctly I would recommend to do it differently - e.g. by making it available in a secondary location so you dont need to have servletcontext in the home/dao layer. e.g. put it in a threadlocal/static/etc. and access that.

/max

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Sharing SessionFactory Between - More Info
PostPosted: Wed Mar 22, 2006 1:36 pm 
Newbie

Joined: Wed Feb 22, 2006 1:24 am
Posts: 4
Location: Brazil
Dear Hibernaters,

Considering that no one reply the post I would to give more information about this issue.

The point here is that the SessionFactory object was created with the HibernatePlugIn and this object is stored at the ServletContext under the key HibernatePlugIn.SESSION_FACTORY_KEY

To retrieve this object on a Controller object like MyAction (derived from org.apache.struts.action.Action) it would be easy because the base class has a method getServlet(), which returns an ActionServlet object. Using this object the SessionFactory object can be easily returned, using the code:
getServlet().getServletContext().getAttribute(HibernatePlugIn.SESSION_FACTORY_KEY)

In order to decouple the Controller layer from the Model Layer, let’s consider that the the object from class MyAction will instantiate an object which will represent the service (Service).

Then, this Service object will instantiate an object from the class TableNameHome. This class was created by Hibernate Tools.

Finally, the object from TableNameHome class will retrieve the SessionFactory and performs database operations using Hibernate.

On this scenario, how the SessionFactory could be retrieved at the Model Layer?

One approach could be retrieve the SessionFactory at the MyAction class and use this object as a parameter for the Service class constructor. In this case the constructor would be:
Service (SessionFactory)

Then the Service object would pass the SessionFactor object as a parameter for the TableNameHome class as well.

The problem with this approach is that Controller and Model Layer will have a dependency from each other.
And another problem is that the template used by Hibernate Tools would have to be changed in order to handle the necessary changes to the TableNameHome constructor.

Does any one have another approach to solve this?

Thanks in advance, for you attention and support.

Best regards.


Top
 Profile  
 
 Post subject: Sharing SessionFactory between layers - More info
PostPosted: Wed Mar 22, 2006 1:43 pm 
Newbie

Joined: Wed Feb 22, 2006 1:24 am
Posts: 4
Location: Brazil
Dear Max and other Hibernaters,

Initially I would like to thank your attention.

I posted more information about the problem, maybe it can help you to understand it better.

Where Can I find more information about this threadlocal/static/etc approach?

Finally, let me know how can I rate your support.

Best regards,


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 22, 2006 2:41 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Just search for threadlocal hibernate...also alot on it on our wiki and in Hibernate In Action.

too rate: just click the "rate" link under the relevant posting

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.