4 Replies Latest reply on Mar 19, 2008 3:46 PM by sajhak

    could not insert exception in JBOSS 4.2.2 GA ...

    sajhak

      Hi all ,

      im testing en Entity ( named as BookBank ) , using MySQL.

      I tested with the default database(HSQL) , and it succeeded.
      But when i configure the database to MySQL , it throws the following exception ..
      ( i placed connection pool config file "booksdb-ds.xml" in to the deploy directory of JBOSS and the

      mySQL-J connector jar file to the lib directory )
      when i used with HSQL the -ds.xml file had following properties






      , those properties i didnt include in the booksdb-ds.xml file..
      is that the cause of the problem ?

      pls help

      i have attached my Entity class , persistence.xml and part of the booksdb-ds.xml files for your consideration ...

      also please give me a good resource of "EJB3 persistance , how the configurations should be done in JBOSS , etc etc .." for a beginner

      thanks in advance
      Saji


      19:25:30,000 WARN [JDBCExceptionReporter] SQL Error: 1146, SQLState: 42S02
      19:25:30,000 ERROR [JDBCExceptionReporter] Table 'b04042.bookbank' doesn't exist

      19:25:30,062 ERROR [STDERR] javax.ejb.EJBException: javax.persistence.Persistenc
      eException: org.hibernate.exception.SQLGrammarException: could not insert: [dev.
      saji.ejb.bean.entity.BookBank]
      19:25:30,062 ERROR [STDERR] at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptio
      nInOurTx(Ejb3TxPolicy.java:63)
      19:25:30,062 ERROR [STDERR] at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(T
      xPolicy.java:83)
      19:25:30,062 ERROR [STDERR] at org.jboss.aspects.tx.TxInterceptor$Required.i
      nvoke(TxInterceptor.java:191)
      19:25:30,062 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invo
      keNext(MethodInvocation.java:101)
      19:25:30,062 ERROR [STDERR] at org.jboss.aspects.tx.TxPropagationInterceptor
      .invoke(TxPropagationInterceptor.java:95)
      19:25:30,062 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invo
      keNext(MethodInvocation.java:101)




      BookBank.java
      --------------
      package dev.saji.ejb.bean.entity;

      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.Table;
      import java.io.Serializable;


      @Entity
      @Table
      public class BookBank implements Serializable {

      long id;
      String title;
      String author;
      double price;



      public BookBank() {
      super();
      }

      public BookBank (String title , String author , double price) {
      super();
      this.title = title;
      this.author = author;
      this.price = price;

      }

      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      public long getId() {
      return id;
      }

      public void setId(long id) {
      this.id = id;
      }

      public String getTitle() {
      return title;
      }

      public void setTitle(String title) {
      this.title = title;
      }

      public String getAuthor() {
      return author;
      }

      public void setAuthor(String author) {
      this.author = author;
      }

      public double getPrice() {
      return price;
      }

      public void setPrice(double price) {
      this.price = price;
      }
      }
      ==============================================

      persistence.xml
      ---------------
      <?xml version="1.0" encoding="UTF-8"?>

      <persistence-unit name="EntityBean">
      <jta-data-source>java:/EntityBean</jta-data-source>
      dev.saji.ejb.bean.entity.BookBank
      </persistence-unit>



      ====================================


      <local-tx-datasource>
      <jndi-name>EntityBean</jndi-name>
      <connection-url>jdbc:mysql://localhost:3306/b04042</connection-url>
      <driver-class>com.mysql.jdbc.Driver</driver-class>
      <user-name>root</user-name>
      sajith

        • 1. Re: could not insert exception in JBOSS 4.2.2 GA ...
          jaikiran

           

          Table 'b04042.bookbank' doesn't exist


          So does this table exist?


          also please give me a good resource of "EJB3 persistance , how the configurations should be done in JBOSS , etc etc .." for a beginner


          See Chapter 3 at http://docs.jboss.org/ejb3/app-server/reference/build/reference/en/html/index.html

          While posting the logs, code or xml content remember to wrap it in a code block using the Code button in the message editor window and please hit the Preview button to make sure your post is correctly formatted

          • 2. Re: could not insert exception in JBOSS 4.2.2 GA ...
            sajhak

            sorry abt that n il make sure my codings are well formatted here after..

            NO , the table doesnt exist.I didnt create the table. but dont the code above create the table named as bookbank ?? ( i thought the code itself will create the table in first place ) . ( The databse b04042 is there )

            or else do i have to create the table seperately and only the insertion part is done by the code ?

            if this doesnt create the table , then how did i run the previous example with HSQL successfully ?

            how can i create a table from an EJB entity ?

            thks
            Saji

            • 3. Re: could not insert exception in JBOSS 4.2.2 GA ...
              jaikiran

              Try adding the hibernate.hbm2ddl.auto property to the persistence.xml file. See the link that i posted earlier for an example and description of this property.

              <properties>
               <property name="hibernate.hbm2ddl.auto" value="update"/>
               </properties>


              • 4. Re: could not insert exception in JBOSS 4.2.2 GA ...
                sajhak

                thanks to u , i got it right this time ...

                and thought of describing the method i followed for the help of any other , who might need this in the future..

                what i did was i add the following property to persistence.xml

                <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />



                also , as u said ,
                <property name="hibernate.hbm2ddl.auto" value="update"/>

                too..

                so this works fine.Even i restart the JBOSS server , the table is persistent in the database..

                But , if i used
                <property name="hibernate.hbm2ddl.auto" value="create-drop"/>

                instead. ( which i tested by using it earlier ) and , when the JBOSS restarted the table will be created from the beginning , when the JBOSS is shutdown , the table is vanished.

                regds
                Saji