您如何以编程方式生成Hibernate JPA模式? [英] How do you programmatically generate a Hibernate JPA schema?

查看:101
本文介绍了您如何以编程方式生成Hibernate JPA模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Hibernate/HBM2DDL模式生成作为使用Liquibase或Flyway之类的工具管理应用程序的SQL模式的起点.为此,我需要在我的项目中运行一个小的实用程序,该实用程序将打印出自动生成的模式.

I'd like to use the Hibernate/HBM2DDL schema generation as a starting point for managing my application's SQL schema using a tool like Liquibase or Flyway. To assist with that, I need a small utility in my project that I can run that will print out the auto-generated schema.

对于较旧的版本或Hibernate,这相对简单.像下面这样的东西会起作用:

With older versions or Hibernate, this was relatively simple. Something like the following would work:

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开始,不能从Hibernate Configuration实例构建SchemaExport实用程序.

But as of at least Hibernate 5.2, the SchemaExport utility can no be built from a Hibernate Configuration instance.

那么今天怎么办呢?

推荐答案

我认为没有充分的理由不通过

I see no good reason to not use standard JPA, via

Persistence.generateSchema(String persistenceUnitName, Map properties);

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

and that way you don't tie yourself to any particular implementation, and still can get a DDL script by use of the javax.persistence.schema-generation.* properties.

这篇关于您如何以编程方式生成Hibernate JPA模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆