首页 > 解决方案 > Liquibase + Spring Data:如何从 JPA 创建实体

问题描述

是否有可能从 @Entity 声明创建 Liquibase 脚本?

我想要的是?当我运行“gradle diff I suppouse liquibase check there is no tableOwner on database, and create record inchangelog”文件时,然后根据该记录在数据库中创建表。

我尝试使用这个库: https ://github.com/liquibase/liquibase-hibernate/wiki

构建.gradle:

buildscript {
    dependencies {
        classpath "org.liquibase:liquibase-gradle-plugin:2.0.2"
    }
}

plugins {
    id 'org.springframework.boot' version '2.2.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
    id "org.liquibase.gradle" version "2.0.2"
}

apply plugin: 'org.liquibase.gradle'

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    compileOnly 'org.projectlombok:lombok'
    implementation 'org.liquibase:liquibase-core'
    runtimeOnly 'javax.xml.bind:jaxb-api'
    compile group: 'io.springfox', name: 'springfox-swagger2', version: swaggerVersion
    compile group: 'io.springfox', name: 'springfox-swagger-ui', version: swaggerVersion
    runtimeOnly 'mysql:mysql-connector-java'
    liquibaseRuntime 'org.liquibase:liquibase-core'
    liquibaseRuntime 'mysql:mysql-connector-java'
    liquibaseRuntime('org.liquibase.ext:liquibase-hibernate5:3.8')
    liquibaseRuntime('org.springframework.boot:spring-boot-starter')
    liquibaseRuntime('org.springframework.boot:spring-boot-starter-data-jpa')
    annotationProcessor 'org.projectlombok:lombok'
}


liquibase {
    activities {
        main {
            driver 'com.mysql.cj.jdbc.Driver'
            url 'jdbc:mysql://localhost:3306/homebudget'
            username 'user'
            password 'pass'
            changeLogFile 'src/main/resources/db/db.changelog-master.xml'
            defaultSchemaName 'homebudget'
            logLevel 'debug'
            changeLogFile 'src/main/resources/db/db.changelog-master.xml'
            referenceUrl 'hibernate:spring:com.homebudget.domain?dialect=org.hibernate.dialect.MySQL5Dialect&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy'
        }
    }
}

在我的项目中,我有实体: com.homebudget.domain.Owner

@Getter
@Setter
@Entity
public class Owner extends BaseEntity {
    private String name;
}

当我跑步时,gradle diff我收到消息:

Reference Database: null @ hibernate:spring:pl.homebudget.domain?dialect=org.hibernate.dialect.MySQL5Dialect&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy (Default Schema: HIBERNATE)
Comparison Database: user@172.21.0.1 @ jdbc:mysql://localhost:3306/homebudget (Default Schema: homebudget)
Compared Schemas: HIBERNATE -> homebudget
Product Name:
     Reference:   'Hibernate'
     Target: 'MySQL'
Product Version:
     Reference:   '5.4.9.Final'
     Target: '8.0.18'
Missing Catalog(s): NONE
Unexpected Catalog(s): NONE
Changed Catalog(s): 
     HIBERNATE
          name changed from 'HIBERNATE' to 'homebudget'
Missing Column(s): NONE
Unexpected Column(s): 
     homebudget.DATABASECHANGELOG.AUTHOR
     homebudget.DATABASECHANGELOG.COMMENTS
     homebudget.DATABASECHANGELOG.CONTEXTS
     homebudget.DATABASECHANGELOG.DATEEXECUTED
     homebudget.DATABASECHANGELOG.DEPLOYMENT_ID
     homebudget.DATABASECHANGELOG.DESCRIPTION
     homebudget.DATABASECHANGELOG.EXECTYPE
     homebudget.DATABASECHANGELOG.FILENAME
     homebudget.DATABASECHANGELOG.ID
     homebudget.DATABASECHANGELOGLOCK.ID
     homebudget.DATABASECHANGELOG.LABELS
     homebudget.DATABASECHANGELOG.LIQUIBASE
     homebudget.DATABASECHANGELOGLOCK.LOCKED
     homebudget.DATABASECHANGELOGLOCK.LOCKEDBY
     homebudget.DATABASECHANGELOGLOCK.LOCKGRANTED
     homebudget.DATABASECHANGELOG.MD5SUM
     homebudget.DATABASECHANGELOG.ORDEREXECUTED
     homebudget.DATABASECHANGELOG.TAG
Changed Column(s): NONE
Missing Foreign Key(s): NONE
Unexpected Foreign Key(s): NONE
Changed Foreign Key(s): NONE
Missing Index(s): NONE
Unexpected Index(s): 
     PRIMARY UNIQUE  ON homebudget.DATABASECHANGELOGLOCK(ID)
Changed Index(s): NONE
Missing Primary Key(s): NONE
Unexpected Primary Key(s): 
     PRIMARY on homebudget.DATABASECHANGELOGLOCK(ID)
Changed Primary Key(s): NONE
Missing Sequence(s): NONE
Unexpected Sequence(s): NONE
Changed Sequence(s): NONE
Missing Table(s): NONE
Unexpected Table(s): 
     DATABASECHANGELOG
     DATABASECHANGELOGLOCK
Changed Table(s): NONE
Missing Unique Constraint(s): NONE
Unexpected Unique Constraint(s): NONE
Changed Unique Constraint(s): NONE
Missing View(s): NONE
Unexpected View(s): NONE
Changed View(s): NONE
19:52:21.564 INFO  [liquibase.integration.commandline.Main]: Liquibase command 'diff' was executed successfully.

标签: javaspringhibernatejpaliquibase

解决方案


推荐阅读