Liquibase 和 JPA 注释实体

     2023-03-31     197

关键词:

【中文标题】Liquibase 和 JPA 注释实体【英文标题】:Liquibase and JPA annotated entities 【发布时间】:2015-01-10 14:17:03 【问题描述】:

我正在尝试将 Liquibase 与 JPA 注释一起使用,但我似乎无法正常工作。

我的项目中有一个没有表和一个 JPA 实体的干净数据库。当我运行 liquibase diff 时,它声称该数据库是最新的 - 但事实并非如此。

liquibase 插件的 maven 配置:

  <plugins>
        <plugin>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-maven-plugin</artifactId>
            <version>3.3.1</version>
            <configuration>
                <changeLogFile>src/main/resources/liquibase/changelog.xml</changeLogFile>
                <referenceUrl>hibernate:spring:com.mycompany.entity?dialect=org.hibernate.dialect.PostgreSQLDialect</referenceUrl>
                <driver>org.postgresql.Driver</driver>
                <url>jdbc:postgresql://localhost:5432/liqubase-test</url>
                <username>postgres</username>
                <password>postgres</password>
            </configuration>
            <executions>
                <execution>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>update</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>

                <dependency>
                    <groupId>org.liquibase.ext</groupId>
                    <artifactId>liquibase-hibernate4</artifactId>
                    <version>3.4</version>
                </dependency>

                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-beans</artifactId>
                    <version>4.1.4.RELEASE</version>
                </dependency>

            </dependencies>
        </plugin>
    </plugins>

changelog.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd">



</databaseChangeLog>

当我运行 generateChangeLog 任务时:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building liqubase-sample 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- liquibase-maven-plugin:3.3.1:generateChangeLog (default-cli) @ liqubase-sample ---
[INFO] ------------------------------------------------------------------------
[INFO] Executing on Database: jdbc:postgresql://localhost:5432/liqubase-test
[INFO] Generating Change Log from database postgres @ jdbc:postgresql://localhost:5432/liqubase-test (Default Schema: public)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.3.xsd http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd"/>
[INFO] Output written to Change Log file, null
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.213 s
[INFO] Finished at: 2015-01-10T14:47:24+01:00
[INFO] Final Memory: 9M/216M
[INFO] ------------------------------------------------------------------------

【问题讨论】:

【参考方案1】:

Liquibase 正在正确地将目标数据库架构与您指定的更改日志进行比较......这是空的 :-)

您需要做一些额外的工作。以下文章似乎很好地概述了使用 Hibernate 的 liquibase 工作流程:

http://www.operatornew.com/2012/11/automatic-db-migration-for-java-web.html

它看起来过于复杂,但 liquibase 所做的是捕获对架构的所有更改。简而言之,您需要将 DB 带到最新的捕获状态,生成 delta,然后将此 delta 添加到您的源代码中。

我希望这会有所帮助。

【讨论】:

参考 URL 的 Liquibase JPA 配置

】参考URL的LiquibaseJPA配置【英文标题】:LiquibaseJPAconfigurationofthereferenceUrl【发布时间】:2013-11-1713:37:54【问题描述】:我正在努力使用liquibase针对我的数据库构建JPA带注释的类差异变更集。尽管如此,我还是对一些事情感到困惑... 查看详情

引用 JDBC 持久化的非 JPA 实体的 JPA 注释实体

】引用JDBC持久化的非JPA实体的JPA注释实体【英文标题】:JPAAnnotatedEntitiesthatReferenceJDBCPersisted,non-JPAEntities【发布时间】:2014-03-2119:01:56【问题描述】:我有一个新项目将使用JPA,并可能使用Hibernate或spring-data作为提供者。但是,... 查看详情

从 JPA 注释的实体类自动生成数据模式

】从JPA注释的实体类自动生成数据模式【英文标题】:AutogeneratedataschemafromJPAannotatedentityclasses【发布时间】:2010-09-2218:49:02【问题描述】:我正在使用JPA(Hibernate的实现)来注释实体类以持久保存到关系数据库(MySQL或SQLServer)... 查看详情

使用注释向 JPA 或 Hibernate 实体列添加约束

】使用注释向JPA或Hibernate实体列添加约束【英文标题】:AddingaconstrainttoaJPAorHibernateentitycolumnusingannotations【发布时间】:2011-04-0421:24:39【问题描述】:我想要一个实体列,它只接受一组枚举值中的一个。例如,假设我有一个带有... 查看详情

克隆 JPA 实体,包括用 spring 的 createdBy 注释的值

】克隆JPA实体,包括用spring的createdBy注释的值【英文标题】:CloningJPAentityincludingvalueannotatedwithspring\'screatedBy【发布时间】:2019-01-2104:40:39【问题描述】:我有以下超类(抽象)publicabstractclassAbstractAuditEntity@CreatedByprivateStringcreated... 查看详情

JOOQ + JPA 实体

...我添加了JPA存储库,并且还在JOOQ生成的类中添加了@Entity注释。现在我仍然想在某些情况下使用JOOQ(使用过滤器和排序和分页查询列表)。但是出了点问题,现在在JOOQ 查看详情

Spring Boot:如何从 JPA/Hibernate 注释中保持 DDD 实体的清洁?

】SpringBoot:如何从JPA/Hibernate注释中保持DDD实体的清洁?【英文标题】:SpringBoot:HowtokeepDDDentitiescleanfromJPA/HibernateAnnotations?【发布时间】:2020-01-2022:22:52【问题描述】:我正在编写一个我希望遵循DDD模式的应用程序,典型的实体... 查看详情

您如何决定在哪些实体类中放置 JPA NamedQuery 注释?

】您如何决定在哪些实体类中放置JPANamedQuery注释?【英文标题】:HowdoyoudecideinwhichentityclassestoputJPANamedQueryannotations?【发布时间】:2010-01-0522:56:00【问题描述】:假设我有一个Customer-CustomerOrder与持有每个订单总价值的CustomerOrder... 查看详情

Liquibase 看不到实体的变化

】Liquibase看不到实体的变化【英文标题】:Liquibasedonotseechangesinentities【发布时间】:2015-01-2309:16:05【问题描述】:我正在尝试使用liquibase配置简单的maven项目。我有很多关于liquibase-maven-plugin和liquibase-hibernate4版本的问题,它们不... 查看详情

Liquibase / Spring Boot / Postgres - 模式名称不起作用

】Liquibase/SpringBoot/Postgres-模式名称不起作用【英文标题】:Liquibase/SpringBoot/Postgres-Schemanamenotworking【发布时间】:2019-04-2018:32:49【问题描述】:我正在使用SpringBoot2.0.2、Liquibase3.5.5和PostgreSQL10。我能够成功地在数据库和我的JPA实... 查看详情

java使用jpa注释的项目实体与供应商建立多对一关系(代码片段)

查看详情

JPA 注释中的 inverse=true

】JPA注释中的inverse=true【英文标题】:inverse=trueinJPAannotations【发布时间】:2011-06-1909:56:01【问题描述】:在我的应用程序中,我使用JPA2.0和Hibernate作为持久性提供程序。我在两个实体之间有一对多的关系(使用@JoinColumn而不是@Jo... 查看详情

使用 liquibase 转义 SQL 文件中的注释

】使用liquibase转义SQL文件中的注释【英文标题】:EscapeforcommentsinSQLfileswithliquibase【发布时间】:2013-05-1611:53:44【问题描述】:我正在使用liquibase和hsqldb数据库。我有一个.sql文件,其中包含用于在引导程序中填充数据库的“插入... 查看详情

flowable入门系列文章70-jpa介绍(代码片段)

...只有符合以下条件的实体才受支持:实体应该使用JPA注释进行配置,我们同时支持字段和属性访问。映射的超类也可以使用。实体应该有一个主键注释@Id,复合主键不被支持(@EmbeddedId和@IdClass)。Id... 查看详情

如何使用spring数据jpa@query注释直接获取学生列表

...义一个从JpaRepository扩展的GroupRepository,并希望使用@Query注释声明一个方法,直接获取给定GroupId的Student列表。怎么样?方法返回值应该是List或Page,不知道如何使用查询语言定义。我知道如何让一个Group实体急切地获取该组的所... 查看详情

使用 @OneToMany 和 @ManyToOne 注释是更新一个实体表,而不是许多表

】使用@OneToMany和@ManyToOne注释是更新一个实体表,而不是许多表【英文标题】:Using@OneToManyand@ManyToOneannotationsisupdatingoneentitytablebutnotthemanytable【发布时间】:2020-07-1815:08:26【问题描述】:我有一个SpringRESTfulAPI,它使用H2数据库和J... 查看详情

如何在运行时检索 JPA 中实体的映射表名称?

...题描述】:是否可以确定实体的本机表名?如果存在Table注释,这很容易:entityClass.getAnnotation(Table.class).name()但是如果没有Table注释呢?Hibernate通过Co 查看详情

Hibernate/JPA 注释中的多列连接

】Hibernate/JPA注释中的多列连接【英文标题】:Multi-ColumnJoininHibernate/JPAAnnotations【发布时间】:2012-06-0610:23:22【问题描述】:我有两个实体,我想通过多个列加入它们。这些列由两个实体共享的@Embeddable对象共享。在下面的示例中... 查看详情