How to generate schema for JPA entity

为君一笑 提交于 2020-01-03 05:13:20

问题


I am having a JPA entity for "User". I want to generate a sql statement for this entity using maven hibernate3 Plugin. I tried using persistence.xml as configured in https://stackoverflow.com/questions/6855119/how-to-generate-schema-through-hibernate3hbdml-in-persistence-xml but my configuration fails. How to configure persistence.xml with any simple database and access the table created using maven hibernate3:hbm2ddl plugin.


回答1:


Here is my example configuration for HSQLdb which generates src/main/resources/db-scheme.sql:

From 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



来源:https://stackoverflow.com/questions/6856934/how-to-generate-schema-for-jpa-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!