使用JPA:架构不是在H2中创建的

时间:2014-01-13 17:17:01

标签: hibernate jpa jboss h2

我有一个Maven jar项目。

我已将JBoss standalone.xml 配置如下:

<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>

persistence.xml 文件如下:

<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>

首先,我通过H2控制台进行身份验证。然后我安装我的jar“Maven install”。

但不幸的是,架构并未创建。可能缺少什么?

非常感谢你。

1 个答案:

答案 0 :(得分:1)

请在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>

在persistence.xml中进行更改 创建持久性单元和事务类型。 创建和属性。

<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>

创建一个java类HibernateUtil.java 并提供持久性单元名称 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;
    }

}