【问题标题】:Generate DDL with Hibernate 4 without connection在没有连接的情况下使用 Hibernate 4 生成 DDL
【发布时间】:2018-12-12 23:24:36
【问题描述】:

我曾经使用 hibernate3-maven-plugin 和 Hibernate 3 生成 DDL。 由于我切换到 Hibernate 4,我不能再使用这个插件了。

如果没有连接到实际的 PostgreSQL 数据库,我找不到从映射生成 DDL 的方法。

我尝试了很多东西(包括来自 de.juplo 和 this 的 hibernate4-maven-plugin),但我觉得每种方法都需要数据库连接。

有没有(好的)方法在 Hibernate 4 中生成 DDL 而无需连接?

【问题讨论】:

    标签: java postgresql hibernate maven ddl


    【解决方案1】:

    我可以通过这种方式生成我的 DDL 脚本:

    <plugin>
                    <groupId>de.juplo</groupId>
                    <artifactId>hibernate4-maven-plugin</artifactId>
                    <version>1.1.1</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>export</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <hibernateDialect>org.hibernate.dialect.PostgreSQLDialect</hibernateDialect>
    
                        <!-- I want generate the schemas for these dialects too, at same time... -->
                        <!-- <hibernateDialect>org.hibernate.dialect.Oracle10gDialect</hibernateDialect> -->
                        <!-- <hibernateDialect>org.hibernate.dialect.SQLServerDialect</hibernateDialect> -->
    
                        <target>SCRIPT</target>
                    </configuration>
                </plugin>
    

    我不需要任何连接,我只是指定 SQL 方言。使用mvn clean package Maven 将脚本放置在目标文件夹中。

    这是我的persistence.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.1"
       xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
            http://xmlns.jcp.org/xml/ns/persistence
            http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
       <persistence-unit name="primary">
          <jta-data-source>java:jboss/datasources/library.backendDS</jta-data-source>
          <properties>
             <!-- Properties for Hibernate -->
             <property name="hibernate.hbm2ddl.auto" value="validate" />
             <property name="hibernate.show_sql" value="false" />
          </properties>
       </persistence-unit>
    </persistence>
    

    【讨论】:

    • 这正是我尝试过的。但是我有这个错误:解析 JNDI 名称时出错 [java:comp/env/jdbc/myapp]。我想那是因为我的 hibernate.cfg.xml 中有这个:&lt;property name="connection.datasource"&gt;java:comp/env/jdbc/myapp&lt;/property&gt;
    • 我认为你不需要 cfg.xml ......你是 JEE 项目吗?
    • 是的。我发现绕过此错误的唯一方法是在我的 hibernate.cfg.xml 中注释此属性。
    • 我已经更新了我的答案。尝试在您的 persistence.xml 中设置数据源。另外,你能发布你的 persistence.xml 和 hibernate.cfg.xml 吗?
    • 我没有 persistence.xml。通过在我的 hibernate.cfg.xml。相反,我在构建会话工厂之前这样做了:configuration.setProperty("hibernate.connection.datasource", "java:comp/env/jdbc/myapp"); 现在可以工作了,谢谢。
    猜你喜欢
    • 2015-09-01
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 2014-07-04
    • 1970-01-01
    • 2022-09-24
    • 2023-03-10
    • 2010-10-01
    相关资源
    最近更新 更多