如何为JPA实体生成架构 [英] How to generate schema for JPA entity

查看:86
本文介绍了如何为JPA实体生成架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

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

来自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

这篇关于如何为JPA实体生成架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆