如何以编程方式生成Hibernate JPA架构?

时间:2017-06-24 14:53:26

标签: java hibernate jpa hbm2ddl

我希望使用Hibernate / HBM2DDL模式生成作为使用Liquibase或Flyway等工具管理应用程序的SQL模式的起点。为了帮助解决这个问题,我需要在我的项目中使用一个小工具,它可以打印出自动生成的模式。

对于旧版本或Hibernate,这相对简单。以下内容可行:

EntityManagerFactory emf = null; // TODO: create your EMF the usual way.
Class<? extends Dialect> hibernateDialectType = null; // TODO: e.g. HSQLDialect.class.

Configuration hibernateConfig = new Configuration();
hibernateConfig.setProperty(Environment.DIALECT, hibernateDialectType.getName());
for (EntityType<?> entityType : emf.getMetamodel().getEntities()) {
        hibernateConfig.addAnnotatedClass(entityType.getJavaType());
}

SchemaExport schemaExporter = new SchemaExport(hibernateConfig);
schemaExporter.setFormat(true);
schemaExporter.setDelimiter(";");
schemaExporter.create(Target.SCRIPT);

但至少从Hibernate 5.2开始,SchemaExport实用程序无法从Hibernate Configuration实例构建。

那么今天怎么办呢?

3 个答案:

答案 0 :(得分:2)

我认为没有充分理由不使用标准JPA,通过

Persistence.generateSchema(String persistenceUnitName, Map properties);

这样你就不会将自己绑定到任何特定的实现,并且仍然可以使用javax.persistence.schema-generation.*属性来获取DDL脚本。

答案 1 :(得分:1)

在深入了解Hibernate Ant task's source on GitHub之后,我提出了以下解决方案:

/**
 * Uses Hibernate's HBM2DDL {@link SchemaExport} utility to generate SQL
 * database schemas.
 */
public final class HibernateSchemaPrinter {
    /**
     * A small application driver that calls
     * {@link #printHibernateSchemaToStdout(String, Class)}.
     * 
     * @param args
     *            (unused)
     */
    public static void main(String[] args) {
        printHibernateSchemaToStdout("gov.hhs.cms.bluebutton.data", PostgreSQL95Dialect.class);
    }

    /**
     * Prints the Hibernate-/HDM2DDL- auto-generated SQL schema to
     * {@link System#out}.
     * 
     * @param persistenceUnitName
     *            the name of the JPA persistence unit to generate the schema
     *            for
     * @param dialectType
     *            the Hibernate {@link Dialect} type to generate the schema for,
     *            e.g. {@link PostgreSQL95Dialect}
     */
    public static void printHibernateSchemaToStdout(String persistenceUnitName, Class<? extends Dialect> dialectType) {
        Map<Object, Object> properties = new HashMap<>();
        properties.put(AvailableSettings.DIALECT, dialectType.getName());

        /*
         * Use a Hibernate EntityManagerFactoryBuilderImpl to create a JPA
         * EntityManagerFactory, then grab the (now populated) Hibernate
         * Metadata instance out of it.
         */
        EntityManagerFactoryBuilderImpl entityManagerFactoryBuilder = new CustomHibernatePersistenceProvider()
                .getEntityManagerFactoryBuilder(persistenceUnitName, properties);
        entityManagerFactoryBuilder.build();
        Metadata metadata = entityManagerFactoryBuilder.getMetadata();

        SchemaExport schemaExport = new SchemaExport();
        schemaExport.setHaltOnError(true);
        schemaExport.setFormat(true);
        schemaExport.setDelimiter(";");
        schemaExport.execute(EnumSet.of(TargetType.STDOUT), Action.CREATE, metadata);
    }

    /**
     * A small hack, needed to extract the
     * {@link EntityManagerFactoryBuilderImpl} from
     * {@link HibernatePersistenceProvider}. Taken from the Hibernate Ant task
     * here: <a href=
     * "https://github.com/hibernate/hibernate-tools/blob/321dba082f0cd11a2295063e0cbcf4f34a5b8bdd/main/src/java/org/hibernate/tool/ant/JPAConfigurationTask.java">
     * JPAConfigurationTask.java</a>.
     */
    private static final class CustomHibernatePersistenceProvider extends HibernatePersistenceProvider {
        /**
         * (See overridden method; we're just making it <code>public</code>.)
         * 
         * @param persistenceUnit
         *            (see overridden method)
         * @param properties
         *            (see overridden method)
         * @return (see overridden method)
         */
        public EntityManagerFactoryBuilderImpl getEntityManagerFactoryBuilder(String persistenceUnit,
                Map<Object, Object> properties) {
            return (EntityManagerFactoryBuilderImpl) getEntityManagerFactoryBuilderOrNull(persistenceUnit, properties);
        }
    }
}

这些天需要更多代码,但仍然有效,非常好。

答案 2 :(得分:1)

如果您希望直接从Hibernate元数据生成Liquibase更改日志,则可以使用以下代码:

// Create a "connection" to the offline JPA data.
String url = "jpa:persistence:META-INF/persistence.xml";
Database jpaDatabase = CommandLineUtils.createDatabaseObject(RESOURCE_ACCESSOR, url, null, null, null, null,
        null, false, false, null, null, null, null, null, null, null);

DiffResult schemaDiff = DiffGeneratorFactory.getInstance().compare(jpaDatabase, null,
        CompareControl.STANDARD);
DiffToChangeLog diffChangeLogProducer = new DiffToChangeLog(schemaDiff, new DiffOutputControl());
diffChangeLogProducer.print(System.out);