Hibernate ToolsのAnt Pluginを使ってmaven-antrun-pluginをたたく。hibernate3-maven-pluginは公式プラグインではなく更新されていない。
pom.xmlに以下のdependencyを追加して、
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-tools</artifactId>
<version>4.3.1.CR1</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
Hibernate4.2(JPA2.0)系を使っていのであればhibernate-toolsのバージョンを4.0.0-CR1
にしないとダメっぽい。
以下のようにantrun-pluginの設定を追加。
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<!-- Hibernatetool will generate everything before running tests -->
<phase>test-compile</phase>
<configuration>
<target>
<echo message="[gen-ddl] start!"/>
<property name="maven_compile_classpath" refid="maven.compile.classpath"/>
<property name="maven_test_classpath" refid="maven.test.classpath"/>
<path id="hibernatetool.path">
<pathelement path="${maven_compile_classpath}"/>
<pathelement path="${maven_test_classpath}"/>
</path>
<taskdef name="hibernatetool"
classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="hibernatetool.path"/>
<property name="ddl.generated.directory"
value="${project.build.directory}/generated-ddl"/>
<mkdir dir="${ddl.generated.directory}"/>
<hibernatetool destdir="${ddl.generated.directory}">
<classpath>
<path location="${project.build.directory}/classes"/>
</classpath>
<jpaconfiguration persistenceunit="gen-ddl"/>
<hbm2ddl export="false" outputfilename="generated-ddl.sql" format="true"/>
</hibernatetool>
<echo message="[gen-ddl] completed!"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
以下のsrc/main/resources/persistence.xmlを作成(すでに存在する場合は<persistence-unit name="gen-ddl">...</persistence-unit>
を追加)。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="gen-ddl"
transaction-type="RESOURCE_LOCAL">
<properties>
<!--<property name="hibernate.ejb.naming_strategy"-->
<!--value="org.springframework.boot.orm.jpa.SpringNamingStrategy"></property>-->
<property name="hibernate.ejb.naming_strategy"
value="org.hibernate.cfg.ImprovedNamingStrategy"></property>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"></property>
</properties>
</persistence-unit>
</persistence>
hibernate.dialect
の部分に対象のDBに対応したDialectを指定する。
あとは
mvn test -Dmaven.test.skip=true
でtarget/generated-ddl/generated-ddl.sql
にDDLファイルができる。
JPA2.1から導入されたインデックスの設定にも対応していていいね。
import javax.persistence.*;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "todo", indexes = {
@Index(columnList = "todoTitle"),
@Index(columnList = "createdAt")
})
public class Todo {
@Id
@GeneratedValue
private Long todoId;
private String todoTitle;
private boolean finished = false;
@Temporal(TemporalType.TIMESTAMP)
private Date createdAt;
@Version
private Long version;
}
こういうEntityを定義すると、↓のようなDDLが出力される。
create table todo (
todo_id bigint not null auto_increment,
created_at datetime,
finished bit not null,
todo_title varchar(255),
version bigint,
primary key (todo_id)
) ENGINE=InnoDB;
create index UK_1wvlyean1whba3mcxi7phfocs on todo (todo_title);
create index UK_l1w8vw94pip9w18mivisdnvf3 on todo (created_at);