java—如何通过代码调用hibernate ddl生成

axzmvihb  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(348)

我很困惑,因为如果我不使用 hibernate.cfg.xml 文件。
在没有xml配置文件的代码中配置会话工厂时,如何让hibernate调用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>
jhkqcmku

jhkqcmku1#

我使用下面的代码,它为我工作。在这段代码中,您需要更改持久单元名称(您可以在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);
    }

相关问题