user3169231
user3169231

Reputation: 257

Using JPA : Schema not created in H2

I have a Maven jar project.

I have configured JBoss standalone.xml as follow :

<datasources>
     <datasource jndi-name="java:jboss/datasources/myDS" pool-name="myDS" enabled="true" use-java-context="true">
        <connection-url>jdbc:h2:tcp://localhost/C:\databases\test</connection-url>                
            <driver>h2</driver>
            <security>
                <user-name>sa</user-name>
            </security>
      </datasource>
      <drivers>
               <driver name="h2" module="com.h2database.h2">
               <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
            </driver>
      </drivers>
</datasources>

And persistence.xml file as follow :

<persistence-unit name="primary" transaction-type="JTA">

    <jta-data-source>java:jboss/datasources/myDS</jta-data-source>

    <class>my.package.Person</class>

    <properties>
        <!-- Properties for Hibernate -->
        <property name="hibernate.hbm2ddl.auto" value="create" />
        <property name="dialect" value="org.hibernate.dialect.H2Dialect" />
        <property name="hibernate.show_sql" value="true" />
    </properties>
</persistence-unit>

First, I authenticate by H2 Console. Then I install my jar "Maven install".

But unfortunately, the schema is not created. What might be missing?

Thank you a lot.

Upvotes: 0

Views: 2545

Answers (1)

Ujwal Daware
Ujwal Daware

Reputation: 391

Please create the data source, security and drivers in your standalone.xml

<subsystem xmlns="urn:jboss:domain:datasources:1.1">
    <datasource jta="true" jndi-name="java:jboss/datasources/myDataSource" pool-name="myDataSource" enabled="true" use-ccm="false">
                    <connection-url>jdbc:mysql://localhost:3306/testDB</connection-url>
                    <driver-class>com.mysql.jdbc.Driver</driver-class>
                    <driver>mysql</driver>
    <security>
             <user-name>root</user-name>
             <password>root</password>
        </security>
    <drivers>
              <driver name="h2" module="com.h2database.h2">
              <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
              </driver>
              <driver name="mysql" module="com.mysql">
              <xa-datasource-class>com.mysql.jdbc.Driver</xa-datasource-class>
              </driver>
        </drivers>
</datasources>
</subsystem>

make a changes in persistence.xml Create persistence unit and transaction type. Create the and properties.

<persistence-unit name="abc" transaction-type="JTA">

    <jta-data-source>java:jboss/datasources/myDataSource</jta-data-source>

    <class>my.package.ClassName</class>

    <properties>
        <!-- Properties for Hibernate -->
        <property name="hibernate.hbm2ddl.auto" value="create" />
        <property name="dialect" value="org.hibernate.dialect.H2Dialect" />
        <property name="hibernate.show_sql" value="true" />
    </properties>
</persistence-unit>

create a java class HibernateUtil.java and provide the persistence unit name to entityManagerFactory = Persistence.createEntityManagerFactory("abc");

public class HibernateUtil {

    private static final EntityManagerFactory entityManagerFactory;
    static {
                try {
                     entityManagerFactory = Persistence.createEntityManagerFactory("abc");
                     System.out.println("Entity Menager Test.............."+ entityManagerFactory);
                } catch (Throwable ex) {

                    System.err.println("Initial SessionFactory creation failed." + ex);
                    throw new ExceptionInInitializerError(ex);

                  }
    }

public static EntityManagerFactory getEntityManagerFactory() {
         return entityManagerFactory;
    }

}

Upvotes: 1

Related Questions