-->
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.  [ 2 posts ] 
Author Message
 Post subject: Best way to initialize Hibernate3 mapping files on startup?
PostPosted: Mon Jan 09, 2006 1:23 pm 
Newbie

Joined: Mon Jan 09, 2006 1:18 pm
Posts: 4
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using: MySQL 5.0

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:



I am currently using a servlet to initilize my hibernate mapping file on server startup. I am using Tomcat 4.1.31. Is this an acceptable way of initializing and if so, am I missing anything in my code below? Thanks!

Code:
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.apache.log4j.Logger;

public class HibernateInitializer extends HttpServlet {
    private static Logger LOG = Logger.getLogger(HibernateInitializer.class);

    public void init(ServletConfig servletConfig) throws ServletException {
        super.init(servletConfig);
        initializeHibernateSessionFactory();
    }

    public void initializeHibernateSessionFactory(){
        LOG.info("Initializing hibernate with servlet...");

        // Initialize the Hibernate Factory
        Session session = null;

        try {
            session = HibernateHelper.getSession();
        } catch (HibernateException e) {
           throw new RuntimeException("Could not initiaze the Hibernate Session Factory", e);
        }
        finally{
            try {
                session.flush();
                session.close();
                LOG.info("Hibernate initialization complete.");

            } catch (HibernateException e) {
                throw new RuntimeException("Could not finish the Hibernate session after initializing", e);
            }
        }
    }
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 1:48 pm 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
Using a Servlet Filter is one of the most common approaches unless you are using another framework like Spring. Refer to
http://www.hibernate.org/43.html
for implementation details.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.