使用带有注释和ANT的模型类在Hibernate中导出DDL模式

时间:2014-07-05 01:44:45

标签: hibernate ant annotations schema ddl

我正在使用带注释的模型类。 我们需要使用这些类创建模式sql。但找不到任何办法。 stackoverflow上有很多关于此的线程,但它们都不适用于我。

其中一个主题是Schema export with hibernate annotations

1 个答案:

答案 0 :(得分:0)

这个怎么样:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>generate-test-sql-scripts</id>
            <phase>generate-test-sources</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <tasks>
                    <property name="maven_test_classpath" refid="maven.test.classpath"/>
                    <path id="hibernate_tools_path">
                        <pathelement path="${maven_test_classpath}"/>
                    </path>
                    <property name="hibernate_tools_classpath" refid="hibernate_tools_path"/>
                    <taskdef name="hibernatetool"
                             classname="org.hibernate.tool.ant.HibernateToolTask"/>
                    <mkdir dir="${project.build.directory}/test-classes/hsqldb"/>
                    <hibernatetool destdir="${project.build.directory}/test-classes/hsqldb">
                        <classpath refid="hibernate_tools_path"/>
                        <jpaconfiguration persistenceunit="testPersistenceUnit"
                                          propertyfile="src/test/resources/META-INF/spring/jdbc.properties"/>
                        <hbm2ddl drop="false" create="true" export="false"
                                 outputfilename="create_db.sql"
                                 delimiter=";" format="true"/>
                        <hbm2ddl drop="true" create="false" export="false"
                                 outputfilename="drop_db.sql"
                                 delimiter=";" format="true"/>
                    </hibernatetool>
                </tasks>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-tools</artifactId>
            <version>${hibernate.tools.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-commons-annotations</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
    </dependencies>
</plugin>