您如何以编程方式生成Hibernate JPA模式?

编程入门 行业动态 更新时间:2024-10-09 05:15:07
本文介绍了您如何以编程方式生成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模式?

本文发布于:2023-11-09 07:54:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1571792.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   方式   Hibernate   JPA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!