我有一堆用hibernate注释注释的类。我正在使用Maven,Hibernate和Spring。如何使用hibernate3-maven-plugin的hbm2ddl生成数据库模式?
答案 0 :(得分: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>
当然,阅读documentation会有所帮助。
答案 1 :(得分:4)
假设您的项目具有以下结构:
. ├── pom.xml └── src ├── main │ ├── java │ │ └── com │ │ └── stackoverflow │ │ └── Foo.java │ └── resources │ └── META-INF │ └── persistence.xml └── test └── 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,请将jpaconfiguration
和persitence.xml
替换为annotationconfiguration
和src/main/resources/hibernate.cfg.xml
。
在获得的输出的摘录下面:
$ mvn process-classes [INFO] Scanning for projects... ... [INFO] --- hibernate3-maven-plugin:2.2:hbm2ddl (default) @ Q4029456 --- ... drop table Foo if exists; create table Foo (id bigint generated by default as identity, name varchar(255), primary key (id)); ... [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ ...