1 Reply Latest reply on Oct 12, 2008 9:15 AM by jaikiran

    creating database using entitymanager

    drinkbythebeach

      Hey,

      I am using JPA along ejb3. My problem lies in automatically creating the database. When the using hibernate, the table is automatically created when the "hbm2ddl.auto" property is set to "create" and a sessionfactory is created. With JPA and ejb3 I am injecting an entitymanager using the @PersistenceContext annotation. The problem is that when an entitymanager is injected and methods are called there is no database created to insert to. My question is how do I go about creating the database automatically when it is being used this way, or what is the correct approach.

      thanks in advance


      This is the code where the entitymanager is being injected.

      import javax.ejb.*;
      import javax.persistence.*;
      
      
      @Stateless
      public class TodoDao implements TodoDaoInt {
      
       @PersistenceContext
       private EntityManager em;
      
       public void persist (Todo todo) {
       em.persist (todo);
       }
      ....................................
      


      This is the servlet where the ejb is being used and the persist method is called but the table does not exist

      protected void processRequest(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
      
       TodoDaoInt tododaoint = this.getDao();
      
       Todo todo = new Todo();
       todo.setTitle("title");
       todo.setDescription("object to persist");
      
       tododaoint.persist(todo);
      
       String destinationPage = "/Results.jsp";
      
       // Redirect to destination page.
       RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(destinationPage);
      
       dispatcher.forward(request, response);
       }
      
       private TodoDaoInt getDao() {
      
       try {
       InitialContext ctx = new InitialContext();
       return (TodoDaoInt) ctx.lookup("ejb3serv2/TodoDao/local");
       } catch (Exception e) {
       e.printStackTrace();
       throw new RuntimeException("couldn't lookup Dao 2", e);
       }
      
       }
      


      This is the persistence.xml of the ejb
      <persistence>
      
      <persistence-unit name="helloworld2">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <jta-data-source>java:/DefaultDS</jta-data-source>
      
      <properties>
      
      <property name="hibernate.archive.autodetection"
      value="class, hbm"/>
      <property name="hibernate.connection.driver_class"
      value="com.mysql.jdbc.Driver" />
      <property name="hibernate.connection.url"
      value="jdbc:mysql://localhost/EJB3"/>
      <property name="hibernate.connection.username"
      value="ded"/>
      <property name="hibernate.connection.password"
      value="dfa"/>
      <property name="hibernate.dialect"
      value="org.hibernate.dialect.MySQLDialect"/>
      
      <!-- Use the C3P0 connection pool provider -->
      <property name="hibernate.c3p0.min_size" value="5"/>
      <property name="hibernate.c3p0.max_size" value="20"/>
      <property name="hibernate.c3p0.timeout" value="300"/>
      <property name="hibernate.c3p0.max_statements" value="50"/>
      <property name="hibernate.c3p0.idle_test_period" value="3000"/>
      
      <!-- Show and print nice SQL on stdout -->
      <property name="show_sql" value="true"/>
      <property name="format_sql" value="true"/>
      <property name="hbm2ddl.auto" value="create"/>
      
      </properties>
      
      
       </persistence-unit>
      </persistence>
      
      


      This is the error when the servlet is called saying there is no table to insert to
      description The server encountered an internal error () that prevented it from fulfilling this request.
      
      exception
      
      javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not insert: [Todo]
      
      ..............................
      root cause
      
      javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not insert: [Todo]
       org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:629)
      
      .......................................................
      root cause
      
      org.hibernate.exception.SQLGrammarException: could not insert: [Todo]
      
      .......................................................
      java.sql.SQLException: Table not found in statement [insert into Todo (description, title) values (?, ?)]
       org.hsqldb.jdbc.Util.throwError(Unknown Source)