7

我有一堆用休眠注释注释的类。我正在使用 Maven、Hibernate 和 Spring。如何使用 hibernate3-maven-plugin 的 hbm2ddl 生成数据库模式?

4

2 回答 2

4

一个像这样的简短示例:

<build>
<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>hibernate3-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
      <components>
        <component>
          <name>hbm2ddl</name>
          <implementation>jdbcconfiguration</implementation>
        </component>
      </components>
      <componentProperties>
        <drop>true</drop>
        <configurationfile>/src/main/resources/hibernate.cfg.xml</configurationfile>
      </componentProperties>
    </configuration>
    <dependencies>
      <dependency>
        <groupId>jdbc.artifact.groupid</groupId>
        <artifactId>jdbc-driver</artifactId>
        <version>1.0</version>
      </dependency>
    </dependencies>
  </plugin>
</plugins>

当然,阅读文档会有所帮助。

于 2010-10-27T06:51:50.267 回答
4

假设您的项目具有以下结构:

.
├── pom.xml
└── 源
    ├── 主要
    │ ├── java
    │ │ └── com
    │ │ └── stackoverflow
    │ │ └── Foo.java
    │ └── 资源
    │ └── META-INF
    │ └──persistence.xml
    └── 测试
        └── java

persistence.xml包含以下内容:

<persistence>
  <persistence-unit name="MyPu" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.stackoverflow.Foo</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
      <property name="hibernate.connection.url" value="jdbc:h2:mem:Q4029456-1.0-SNAPSHOT"/>
      <property name="hibernate.connection.user" value="APP"/>
      <property name="hibernate.connection.password" value="APP"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
    </properties>
  </persistence-unit>
</persistence>

然后以下配置会将架构导出为构建的一部分:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.stackoverflow</groupId>
  <artifactId>Q4029456</artifactId>
  <version>1.0-SNAPSHOT</version>
  <properties>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>3.4.0.GA</version>
    </dependency>
    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <version>1.2.144</version>
    </dependency>
    ...
  </dependencies>
  <repositories>
    <repository>
      <id>repository.jboss.org-public</id>
      <name>JBoss repository</name>
      <url>https://repository.jboss.org/nexus/content/groups/public</url>
    </repository>
  </repositories>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>hibernate3-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <components>
            <component>
              <name>hbm2ddl</name>
              <implementation>jpaconfiguration</implementation>
            </component>
          </components>
          <componentProperties>
            <drop>true</drop>
            <!-- optional, but useful for later inspection -->
            <outputfilename>schema.ddl</outputfilename>
            <persistenceunit>MyPu</persistenceunit>
          </componentProperties>
        </configuration>
        <executions>
          <execution>
            <phase>process-classes</phase>
            <goals>
              <goal>hbm2ddl</goal>
            </goals>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.2.144</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
</project>

如果您不使用 JPA,请替换jpaconfigurationand the persitence.xmlbyannotationconfiguration和 a 。src/main/resources/hibernate.cfg.xml

下面是获得的输出的摘录:

$ mvn 进程类
[INFO] 正在扫描项目...
...
[信息] --- hibernate3-maven-plugin:2.2:hbm2ddl (默认) @ Q4029456 ---
...
如果存在则删除表 Foo;
create table Foo (id bigint 默认生成为identity, name varchar(255), primary key (id));
...
[信息] --------------------------------------------- -------------------------
[信息] 构建成功
[信息] --------------------------------------------- -------------------------
...
于 2010-10-27T07:52:53.333 回答