Wildfly Greeter快速启动项目

时间:2014-11-02 05:13:21

标签: java hibernate jpa wildfly-8

'greeter' WildFly 8 中的快速入门项目,用于演示基本的hibernate数据库和JPA功能作为教程。在项目中,我不明白何时以及如何在H2数据库中创建“USERS”数据库。这是用于数据库创建和设置的两个相关文件:

  1. META-INF / persistence.xml中
  2.    
    <persistence version="2.0"
        xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
            http://java.sun.com/xml/ns/persistence
            http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
        <persistence-unit name="primary">
            <!-- If you are running in a production environment, add a managed 
                data source, this example data source is just for development and testing! -->
            <!-- The datasource is deployed as WEB-INF/greeter-quickstart-ds.xml, 
                you can find it in the source at src/main/webapp/WEB-INF/greeter-quickstart-ds.xml -->
            <jta-data-source>java:jboss/datasources/GreeterQuickstartDS</jta-data-source>
            <properties>
                <!-- Properties for Hibernate -->
                <property name="hibernate.hbm2ddl.auto" value="create-drop" />
                <property name="hibernate.show_sql" value="false" />
            </properties>
        </persistence-unit>
    </persistence>
    
    1. WEB-INF / greeter-quickstart-ds.xml
    2.  
      <datasources xmlns="http://www.jboss.org/ironjacamar/schema"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.jboss.org/ironjacamar/schema http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd">
          <!-- The datasource is bound into JNDI at this location. We reference 
              this in META-INF/persistence.xml -->
          <datasource jndi-name="java:jboss/datasources/GreeterQuickstartDS"
              pool-name="greeter-quickstart" enabled="true" use-java-context="true">
              <connection-url>jdbc:h2:mem:greeter-quickstart;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1</connection-url>
              <driver>h2</driver>
              <security>
                  <user-name>sa</user-name>
                  <password>sa</password>
              </security>
          </datasource>
      </datasources>
      
      1. /wildfly-greeter/src/main/resources/import.sql

        INSERT INTO USERS (ID, USERNAME, FIRSTNAME, LASTNAME) VALUES (-1, 'jdoe', 'John', 'Doe');

        INSERT INTO USERS (ID, USERNAME, FIRSTNAME, LASTNAME) VALUES (-2, 'emuster', 'Erika', 'Mustermann');

      2. 默认情况下,H2数据库系统没有USERS数据库,该数据库由演示使用。那么在这个演示项目中如何创建“USERS”?感谢。

2 个答案:

答案 0 :(得分:0)

由于persistence.xml中的以下设置,您在运行项目时会自动创建USERS数据库:

<property name="hibernate.hbm2ddl.auto" value="create-drop" />

答案 1 :(得分:0)

数据库是由Hibernate在部署时设置的。为了构建数据库模式,Hibernate会扫描所有相关文件,而不仅仅是XML文件,还会扫描类,以获取所需的信息,这就是 Users 表的来源从

以下是greeter快速启动示例中的确切代码,该示例负责 用户 表:

@Entity
// User is a keyword in some SQL dialects!
@Table(name="Users")
public class User {
... etc.

所以,就是这样,他们只是用复数&#39; s &#39;来称呼 用户 。避免与您选择的DBMS发生冲突。