【问题标题】:How to invoke Hibernate DDL generation via code如何通过代码调用 Hibernate DDL 生成
【发布时间】:2021-01-04 14:53:24
【问题描述】:

我很困惑,因为如果我不使用 hibernate.cfg.xml 文件,我无法让 DDL 生成工作。

如何在没有 XML 配置文件的代码中配置会话工厂时休眠以调用 DDL 生成?我在这里缺少什么?

我的会话工厂通过代码生成,其中 DDL 不起作用。

Configuration configuration = new Configuration();
configuration
   .setProperty("hibernate.connection.driver_class", "org.sqlite.JDBC")
   .setProperty("hibernate.connection.url", "jdbc:sqlite:mydb.db")
   .setProperty("hibernate.dialect", "org.hibernate.dialect.SQLiteDialect")
   .setProperty("hibernate.show_sql", "true")
   .setProperty("hibernate.format_sql", "true")
   .setProperty("connection.username", "")
   .setProperty("connection.password", "")
   .setProperty("hibernate.hdm2ddl.auto", "update");

configuration.addAnnotatedClass(Bill.class);            
this.factory = configuration.buildSessionFactory();

通过 hibernate.xml 生成会话工厂。

Configuration configuration = new Configuration();
configuration.configure();

hibernate.cfg.xml(没有 XML 标头)

<hibernate-configuration>
    <session-factory>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="dialect">org.hibernate.dialect.SQLiteDialect</property>
        <property name="connection.driver_class">org.sqlite.JDBC</property>
        <property name="connection.url">jdbc:sqlite:rechnungen.db</property>
        <property name="connection.username"></property>
        <property name="connection.password"></property>

        <property name="hibernate.hbm2ddl.auto">update</property>

        <mapping class="entity.Bill"/>
    </session-factory>
</hibernate-configuration>

【问题讨论】:

标签: java hibernate hibernate-mapping


【解决方案1】:

我正在使用下面的代码,它对我有用。在此代码中,您需要更改您的持久单元名称(您可以在persistent.xml 中找到它)。

import java.io.IOException;
import java.util.Properties;

import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.HSQLDialect;
import org.hibernate.dialect.Oracle10gDialect;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.hbm2ddl.Target;

/**
 * The Class JpaSchemaExport.
 */
@SuppressWarnings("deprecation")
public class JpaSchemaExport {

    /** The Constant DIALECT_ORACLE. */
    private static final String DIALECT_HSQL = "HSQL";

    /** The Constant SCHEMA_NAME. */
    private static final String SCHEMA_NAME = "MYSCHEMA";

    /**
     * The main method.
     *
     * @param args
     *            the arguments
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static void main(String[] args) throws IOException {
        execute(DIALECT_HSQL, "my-persistent-unit", "src/test/resources/ddl-scripts.sql", false);
    }

    /**
     * Execute.
     *
     * @param dialectName
     *            the dialect name
     * @param persistenceUnitName
     *            the persistence unit name
     * @param destination
     *            the destination
     * @param format
     *            the format
     */
    public static void execute(String dialectName, String persistenceUnitName, String destination, boolean format) {
        Ejb3Configuration cfg = new Ejb3Configuration().configure(persistenceUnitName, new Properties());
        cfg.setProperty("hibernate.dialect", Oracle10gDialect.class.getName());

        // Use when you need DDL scripts to setup development machine
        if (DIALECT_HSQL.equalsIgnoreCase(dialectName)) {
            cfg.setProperty("hibernate.dialect", HSQLDialect.class.getName());
        }
        cfg.setProperty("hibernate.default_schema", SCHEMA_NAME);
        Configuration hbmcfg = cfg.getHibernateConfiguration();
        SchemaExport schemaExport = new SchemaExport(hbmcfg);
        schemaExport.setOutputFile(destination);
        schemaExport.setDelimiter(";");
        schemaExport.setFormat(format);
        schemaExport.execute(Target.SCRIPT, SchemaExport.Type.BOTH);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-02
    • 2012-08-23
    • 2011-01-18
    • 2015-11-12
    • 2010-09-05
    • 1970-01-01
    • 2017-02-09
    • 2012-04-09
    相关资源
    最近更新 更多