支持使用 JPA 进行架构迁移吗?
我最近简要地了解了 JPA,我想知道数据库模式迁移以及与您创建的类保持一致的意义何在。
JPA 是否支持这些东西?公用事业?最佳实践?
干杯!
I've been briefly looking at JPA recently, and I was wondering what the deal is with database schema migrations and staying lined up with the classes you've created.
Is there support in JPA for this stuff? Utilities? Best Practises?
Cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不会依赖 JPA 提供程序来更新数据库架构。
检查 Liquibase 寻找一种好的方法。
I won't rely on JPA providers to update the database schema.
Check Liquibase for one of the good approaches.
简短的回答是不。
如果更改 bean,则必须手动迁移现有模式。因此,对于 Rails 风格的数据库迁移,您将不得不寻找其他地方。
不过,您可以轻松地从 Java bean 生成初始ddl。下面的示例说明了使用 EclipseLink 2.0 版创建模式:
这里的关键元素是
这告诉 EclipseLink 删除现有表并从 JPA 映射生成新表。此过程是高度特定于供应商的,因此对于其他 JPA 供应商(Hibernate、OpenJPA...),您必须查阅他们的特定文档。
The short answer is no.
If you change your beans, then you will have to migrate the existing schema by hand. So for Rails style database migrations you will have to look elsewhere.
You can however generate the initial ddl from your Java beans easily. The example below illustrates schema creation with EclipseLink version 2.0:
The key element here is
This tells EclipseLink to drop existing tables and generate new once from your JPA mapping. This procedure is highly vendor specific so for other JPA vendors (Hibernate, OpenJPA...) you will have to consult their specific documentation.
如果您将
generateDdl
设置为Hibernate(如果它是底层实现),那么它会根据您当前的方言生成数据库架构。所以改变方言后它会自动生成数据库。其他 JPA 提供商可能对此有不同的属性。
If you set the
generateDdl
to Hibernate (if it is the underlying implementation), then it generates the database schema according to your current dialect. So after changing the dialect it will automatically generate the database.Other JPA providers may have different properties for this.