如何为JPA实体生成模式

时间:2011-07-28 09:41:13

标签: hibernate maven persistence

我正在为“用户”拥有一个JPA实体。我想使用maven hibernate3 Plugin为这个实体生成一个sql语句。我尝试使用https://stackoverflow.com/questions/6855119/how-to-generate-schema-through-hibernate3hbdml-in-persistence-xml中配置的persistence.xml,但我的配置失败了。如何使用任何简单数据库配置persistence.xml并访问使用maven hibernate3:hbm2ddl插件创建的表。

1 个答案:

答案 0 :(得分:5)

以下是我生成src/main/resources/db-scheme.sql的HSQLdb的示例配置:

来自pom.xml

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>hibernate3-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <components>
            <component>
                <name>hbm2ddl</name>
                <implementation>jpaconfiguration</implementation>
                <outputDirectory>
                    src/main/resources
                </outputDirectory>
            </component>
        </components>
        <componentProperties>
            <console>false</console>
            <format>true</format>
            <jdk5>true</jdk5>
            <propertyfile>
                src/main/resources/database.properties
            </propertyfile>
            <outputfilename>db-scheme.sql</outputfilename>
            <export>false</export>
        </componentProperties>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.5.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.6.5.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
        </dependency>
    </dependencies>
</plugin>

src/main/resources/database.properties

hibernate.formatSql=true
hibernate.hbm2ddl.auto=validate

# needed for hibernate3-maven-plugin
hibernate.dialect=org.hibernate.dialect.HSQLDialect

src/main/resources/META-INF/persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    <persistence-unit name="DefaultPersistenceUnit" transaction-type="RESOURCE_LOCAL" />
</persistence>

HTH