Re: [Hibernate-devel] mvc & lazy loading

2002-08-27 Thread Jon Lipsky
Hi All,

I use a Filter (a new addition in the 2.3 servlet spec) to open and close my
Hibernate sessions.  By doing it this way it doesn't matter if I am using
Velocity or JSP or something else to access Hibernate.  As far as the "view"
is concerned the Hibernate session just exists, and only the Filter has to
worry about opening and closing it.

I was looking at the examples inclued with Hibernate and I was thinking that
maybe an example should be added of using a Filter since it's a good way to
cleanly seperate the creation and closing of the sessions for a web
application.

Jon...

PS - Here is a code snippet to get you started if you want to do it this
way:

package example;

import cirrus.hibernate.Datastore;
import cirrus.hibernate.Session;
import cirrus.hibernate.SessionFactory;
import cirrus.hibernate.Hibernate;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class HibernateFilter implements Filter
{
 static org.apache.log4j.Category log =
org.apache.log4j.Category.getInstance(HibernateFilter.class.getName());

 private Datastore datastore;
 private SessionFactory sessions;

 public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
 {
  try
  {
   // Get the http session id from the request, then we will try to get the
Hiberate
   // Session from the request.  If it doesn't exist, then we will create
it, otherwise
   // we will use the one that already exists.
   String vSessionId =
((HttpServletRequest)request).getSession(true).getId();
   Session vSession = (Session)request.getAttribute(vSessionId);

   if (vSession == null)
   {
vSession = sessions.openSession();
request.setAttribute(vSessionId, vSession);

if (log.isDebugEnabled())
{
 log.debug("Opened hibernate session.");
}
   }
  }
  catch (Exception exc)
  {
   log.error("Error opening Hibernate session.", exc);
  }

  try
  {
   chain.doFilter(request, response);
  }
  finally
  {
   try
   {
String vSessionId = ((HttpServletRequest)request).getSession().getId();
Session vSession = (Session)request.getAttribute(vSessionId);

// Only try to close the connection if it is open, since it might have
been
// closed somewhere else by mistake.
if (vSession.isOpen())
{
 vSession.close();

 if (log.isDebugEnabled())
 {
  log.debug("Closed hibernate session.");
 }
}
   }
   catch (Exception exc)
   {
log.error("Error closing Hibernate session.", exc);
   }
  }
 }

 public void init(FilterConfig aConfig) throws ServletException
 {
  // Initialize your datastore
  datastore = Hibernate.createDatastore();

  // Initialize your the object -> db mappings
  // ...

  // Initialize your session
  sessions = datastore.buildSessionFactory();
 }

 public void destroy()
 {

 }
}



- Original Message -
From: "Christoph Sturm" <[EMAIL PROTECTED]>
To: "Brad Clow" <[EMAIL PROTECTED]>;
<[EMAIL PROTECTED]>
Sent: Tuesday, August 27, 2002 10:47 AM
Subject: Re: [Hibernate-devel] mvc & lazy loading


> Hi Brad!
>
> This subject is an interesting one that I was also thinking of lately.
> I did a test app with maverick (mav.sourceforge.net), and there it was
> really easy. If the controller(=model) implements ModelLifetime, a discard
> function is called when the views are finished and the model is discarded.
> There I closed my session. Other frameworks that just forward to the view
> dont offer this functionality.
> For most of my stuff I use webwork, so I'd like a solution that works
there
> too. I was thinking of closing the session in the finalize method of my
> controller, but then I dont really know when the session will be closed.
> Another possibility would be to to pass the session to velocity, and close
> it in the velocity view servlet after all is rendered.
>
> How did you implement it?
>
> regards
>  chris
> - Original Message -
> From: "Brad Clow" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Tuesday, August 27, 2002 12:38 AM
> Subject: [Hibernate-devel] mvc & lazy loading
>
>
> >
> > to date, we have avoided using lazy loading when writing a web app in
> > one of the standard mvc type frameworks (eg. struts, webwork, etc).
> > this is because objects r typically retrieved, placed in the request
> > attributes, session closed and control is then passed to the view (JSP,
> > velocity, etc).  if the view attempts to access a lazy loaded collection
> > in one of the objects an exception is thrown as the associated session
> > is closed.
> >
> > what do other people do?
> >
> > yesterday, i spent a few hours writing a very simple webapp framework
> > that uses velocity for the view.  it enables the velocity rendering to
> > be done while a session is open.  so far this is working quite well for
> > us.
> >
> > comments?
> >
> > brad
> >
> > > ___
> > > brad clow
> > > chief technical officer
> > > workingmous

[Hibernate-devel] Re: mvc & lazy loading

2002-08-27 Thread Colin Evans
Hi Brad,

I haven't tried this yet, but if I wanted to use lazy loading with Struts, I
would probably override ActionServlet and do something like this, and have
my DAO layer pull the session from the request context as needed:


/**
 * Hibernate Action Servlet
 */
public class HibernateActionServlet
extends ActionServlet
{
public void service(HttpServletRequest request, 
HttpServletResponse response)
{
Session session = null;
try
{
session = factory.openSession();
request.setAttribute("session", session);
super.service(request, response);
}
finally
{
   if(session != null) session.close();
}
}
}


Do other frameworks allow the controller servlet to be overridden?  This
probably is a fairly straightforward way to get lazy loading working.

-Colin

--
Colin Evans
Mobilesys 
www.mobilesys.com


---
This sf.net email is sponsored by: Jabber - The world's fastest growing 
real-time communications platform! Don't just IM. Build it in! 
http://www.jabber.com/osdn/xim
___
Hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel



[Hibernate-devel] (no subject)

2002-08-27 Thread Lenny Sorey





---
This sf.net email is sponsored by: OSDN - Tired of that same old
cell phone?  Get a new here for FREE!
https://www.inphonic.com/r.asp?r=sourceforge1&refcode1=vs3390
___
Hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel



[Hibernate-devel] (no subject)

2002-08-27 Thread Lenny Sorey





---
This sf.net email is sponsored by: OSDN - Tired of that same old
cell phone?  Get a new here for FREE!
https://www.inphonic.com/r.asp?r=sourceforge1&refcode1=vs3390
___
Hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel



[Hibernate-devel] boolean basic type

2002-08-27 Thread Mark Woon
Hi all...

Can anyone tell me what Hibernate expects the db column type to be if I 
specify type="boolean" in the mapping file?

Thanks,
-Mark


---
This sf.net email is sponsored by: OSDN - Tired of that same old
cell phone?  Get a new here for FREE!
https://www.inphonic.com/r.asp?r=sourceforge1&refcode1=vs3390
___
Hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel


Re: [Hibernate-devel] boolean basic type

2002-08-27 Thread Gavin_King/Cirrus%CIRRUS
This mail is probably spam.  The original message has been attached
along with this report, so you can recognize or block similar unwanted
mail in future.  See http://spamassassin.org/tag/ for more details.

Content preview:  Depends upon the database. Check the source of the
  cirrus.hibernate.sql package for the mapping for your particular
  dialect. Please post these kind of questions in the user forum, rather
  than the development mailing list. [...] 

Content analysis details:   (8.10 points, 8 required)
NO_REAL_NAME   (3.0 points)  From: does not include a real name
X_MAILING_LIST (0.0 points)  Has a X-Mailing-List header
FOR_FREE   (6.0 points)  BODY: No such thing as a free lunch (1)
KNOWN_MAILING_LIST (-0.9 points) Email came from some known mailing list software


--- Begin Message ---

Depends upon the database. Check the source of the cirrus.hibernate.sql
package for the mapping for your particular dialect.

Please post these kind of questions in the user forum, rather than the
development mailing list.

Oh, and the bugfix you requested (update() for classes with no property
columns) is now in CVS

peace



---
This sf.net email is sponsored by: OSDN - Tired of that same old
cell phone?  Get a new here for FREE!
https://www.inphonic.com/r.asp?r=sourceforge1&refcode1=vs3390
___
Hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel

--- End Message ---