支持使用 JPA 进行架构迁移吗?

发布于 2024-08-17 14:49:31 字数 95 浏览 2 评论 0原文

我最近简要地了解了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拒绝两难 2024-08-24 14:49:31

我不会依赖 JPA 提供程序来更新数据库架构。
检查 Liquibase 寻找一种好的方法。

I won't rely on JPA providers to update the database schema.
Check Liquibase for one of the good approaches.

十级心震 2024-08-24 14:49:31

简短的回答是

如果更改 bean,则必须手动迁移现有模式。因此,对于 Rails 风格的数据库迁移,您将不得不寻找其他地方。

不过,您可以轻松地从 Java bean 生成初始ddl。下面的示例说明了使用 EclipseLink 2.0 版创建模式:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="JPATestPU" transaction-type="RESOURCE_LOCAL">
        <provider>
            org.eclipse.persistence.jpa.PersistenceProvider
        </provider>
        <class>org.randompage.MyEntity</class>
        <properties>
            <property name="javax.persistence.jdbc.user" value="johndoe"/>
            <property name="javax.persistence.jdbc.password" value="secret"/>
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:~/.h2/testdb;FILE_LOCK=NO"/>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
            <property name="eclipselink.logging.level" value="INFO"/>
        </properties>
    </persistence-unit>
</persistence>

这里的关键元素是

 <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/> 

这告诉 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:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="JPATestPU" transaction-type="RESOURCE_LOCAL">
        <provider>
            org.eclipse.persistence.jpa.PersistenceProvider
        </provider>
        <class>org.randompage.MyEntity</class>
        <properties>
            <property name="javax.persistence.jdbc.user" value="johndoe"/>
            <property name="javax.persistence.jdbc.password" value="secret"/>
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:~/.h2/testdb;FILE_LOCK=NO"/>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
            <property name="eclipselink.logging.level" value="INFO"/>
        </properties>
    </persistence-unit>
</persistence>

The key element here is

 <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/> 

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.

铁憨憨 2024-08-24 14:49:31

如果您将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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文