Matt RaibleMatt Raible is a Web Developer and Java Champion. Connect with him on LinkedIn.

The Angular Mini-Book The Angular Mini-Book is a guide to getting started with Angular. You'll learn how to develop a bare-bones application, test it, and deploy it. Then you'll move on to adding Bootstrap, Angular Material, continuous integration, and authentication.

Spring Boot is a popular framework for building REST APIs. You'll learn how to integrate Angular with Spring Boot and use security best practices like HTTPS and a content security policy.

For book updates, follow @angular_book on Twitter.

The JHipster Mini-Book The JHipster Mini-Book is a guide to getting started with hip technologies today: Angular, Bootstrap, and Spring Boot. All of these frameworks are wrapped up in an easy-to-use project called JHipster.

This book shows you how to build an app with JHipster, and guides you through the plethora of tools, techniques and options you can use. Furthermore, it explains the UI and API building blocks so you understand the underpinnings of your great application.

For book updates, follow @jhipster-book on Twitter.

10+ YEARS


Over 10 years ago, I wrote my first blog post. Since then, I've authored books, had kids, traveled the world, found Trish and blogged about it all.

How do you initialize Hibernate?

If you're using Hibernate in a servlet container, then you're probably using a hibernate.cfg.xml file to configure Hibernate and talk to your JNDI database connection. If you're not, you might want to consider it. My question is, where do you initialize Hibernate? In version 1.2.3, you call Hibernate.configure() to do this, and everything will startup and be ready - providing that hibernate.cfg.xml is in your classpath (WEB-INF/classes).

// Configure Hibernate.
try {
    Hibernate.configure();
    if (log.isDebugEnabled()) {
        log.debug("Hibernate configuration completed...");
    }
} catch (HibernateException h) {
    h.printStackTrace();
    throw new UnavailableException("Error configuring Hibernate: " + h.getMessage());
}

I've been using a StartupServlet that is set to load first, and is also responsible for putting drop-down options into the application scope. However, it has recently come to my attention that I could easily use a ServletContextListener and initialize it (and my drop-downs) in the contextInitialized() method.

So my question is - which is better? From what I can tell, they do the same thing and I've never had any issues with the StartupServlet. Can anyone offer some pros/cons to each approach? Which do you use?

Posted in Java at Mar 06 2003, 04:08:14 PM MST 6 Comments
Comments:

I recently discussed almost this exact same thing in my blog. The ServletContextListener was new-ish (a 2.3 thing), so I hadn't known about it (damn, gotta keep up with the spec better!). Anyway, what I like about it is that it seems more "semantically" correct to me. e.g. what you are doing is not the work of a servlet, you're doing some setup for your context/app. So, why create a servlet that does something once at startup and then just sits there doing nothing the rest of the time. Again, semantically just seems to fit better. In relation to Hibernate, I do something similar. But, I have a "HibernateManager" singleton that does the initialization and then can hand out sessions and so on (my HibernateSession helper class uses this). This way, my data model is web app independant, so that if I need to write a command line utility or Swing app that does something with the data, I have all my Hibernate setup in a single place that they can all use.

Posted by Anonymous on March 06, 2003 at 06:50 PM MST #

load-at-startup is a hack which the ServletContextListener makes unnecessary. Prior to Servlet 2.3 there was no way other than load-at-startup to get this effect. Which one is better? If you can count on a 2.3 compliant server, I'd say use ServletContextListener .

Posted by Anonymous on March 06, 2003 at 08:49 PM MST #

I made ServletContext listener for hibernate about a half year ago. I didn't like it and it didn't work as I wanted it to work (don't remember the drawbacks). Currently I think that the best way is to create servlet filter that uses a ThreadLocal Session. For me it is the most proper way and it is described here: http://hibernate.bluemars.net/43.html My advice: Forget the whole ServletContextListener thing. It's useless.

Posted by Anonymous on March 07, 2003 at 02:33 AM MST #

I think ServletContextListener is the wrong construct for the problem your Filter solves anyway. ServletContextListener is for handling resources at *webapp* startup and shutdown, not during individual HttpRequests.

Posted by Lance on March 07, 2003 at 08:28 AM MST #

Can't comment on ServletContextListener...But you could try the below to initialize Hibernate within a Servlet container...I keep all of my Hibernate OR Queries in 1 class and the below is the fragment that is part of the class definition. It may not be the best design but it keeps all my Hibernate settings in close proximity. Have fun... static { props = new Properties(); /** * Setting the hibernate properties **/ props.put("hibernate.query.substitutions", "true 1, false 0, yes 'Y', no 'N'"); props.put("hibernate.query.imports", "cirrus.hibernate.test"); props.put("hibernate.dialect", "cirrus.hibernate.sql.PostgreSQLDialect"); props.put("hibernate.connection.driver_class", "org.postgresql.Driver"); // Localhost PostgreSQL //props.put("hibernate.connection.url", "jdbc:postgresql://127.0.0.1:5432/mydb"); props.put("hibernate.connection.username", "myuser"); props.put("hibernate.connection.password", "myuser123"); props.put("hibernate.connection.pool_size", "5"); props.put("hibernate.statement_cache.size", "10"); props.put("hibernate.jdbc.use_streams_for_binary", "true"); props.put("hibernate.jdbc.batch_size", "0"); /** * Create the data store access and factory. **/ try { /** * Configure the datastore **/ dataStore = Hibernate.createDatastore().storeInputStream(new MapLoader().readMapToStream("my-mapping.hbm.xml")); /** * Configure the SessionFactory **/ sessions = dataStore.buildSessionFactory(props); } catch (MappingException e) { log.log(Level.WARNING, "", e); } catch (HibernateException e) { log.log(Level.WARNING, "", e); } catch (Exception e) { log.log(Level.WARNING, "", e); } }

Posted by Anonymous on March 07, 2003 at 10:22 AM MST #

Hello Everyone,

I am new to Hybernate.Iwant to use Hybernate with servlet.My Database is MySQL. i am getting error like "java.lang.NoClassDefFoundError: org/dom4j/DocumentException" while configuring sessionFactory. plz give me detail about how to do it. i am using MyEclipse IDE. if possible give me step by step details. Thanks.

Posted by sachin on January 14, 2009 at 01:39 PM MST #

Post a Comment:
  • HTML Syntax: Allowed