【问题标题】:How to generate schema for JPA entity如何为 JPA 实体生成模式
【发布时间】:2011-10-14 23:26:11
【问题描述】:

我有一个“用户”的 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 插件创建的表。

【问题讨论】:

    标签: hibernate maven persistence


    【解决方案1】:

    这是我的 HSQLdb 配置示例,它生成 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

    【讨论】:

      猜你喜欢
      • 2018-12-25
      • 2011-03-03
      • 2010-09-22
      • 2019-05-19
      • 2013-02-02
      • 2020-05-28
      • 2017-06-12
      • 2013-01-31
      • 2013-09-08
      相关资源
      最近更新 更多