Results 1 to 3 of 3

Thread: Perform code on startup of a web application

  1. #1
    Join Date
    Apr 2009
    Posts
    4

    Default Perform code on startup of a web application

    Hi there!

    I would like to perform some code when a web application is starting.

    I want to create database entries if they are not present in the database. So the user can set hibernate.show_sql to create and after the tables were created a process should writes some entries in a table to make the application runnable. Do someone have a tip for me?

    Thanks!

  2. #2
    Join Date
    Jul 2005
    Location
    Idaho
    Posts
    231

    Default

    By 'starting', I assume you mean when you deploy a war. One way to do what you are looking for, is, in a bean config ( applicationContext.xml or xxx-servlet.xml) define an 'init-method'.
    Code:
    <bean id="appStarter" class="my.project.web.FireUpTheDatabase" init-method="init"/>
    In your FireUpTheDatabase.java class:
    Code:
    public class FireUpTheDatabase {
    ...
      public void init () {
        // do whatever you want to do here
      }
    ...
    }
    Good luck - Steve O

  3. #3
    Join Date
    Dec 2008
    Location
    Hyderabad, India
    Posts
    18

    Default Use "update".

    Hi Henry,

    If this is what you want to do at the starting of your web application -->
    a) tables not existing to get created
    b) populate some rows in tables

    If you are using JSP/Servlets, you can think of the following steps -->
    a) Have a <load-on-startup> entry for a servlet.
    b) In its init() method, invoke a method of some DAO class.
    c) This class should have a static block that builds Hibernate's SessionFactory. If you keep "update" for "hibernate.hbm2ddl.auto" property, which ever classes that are mapped in your hbm files but don't have corresponding tables existing in DB, Hibernate will create tables for them automatically.
    d) The rest of the method can continue with save() method calls to insert rows in required tables.

    If you are using Struts --->
    a) Have a Plug-In class ready for Hibernate in which build Hibernate's sessionfactory. "update" value will do the needful there also.

    Hope this is useful.

    Thanks,
    Sreeram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •