gpt4 book ai didi

java - Spring liquibase自定义ImplicitNamingStrategy

转载 作者:行者123 更新时间:2023-12-01 23:42:41 24 4
gpt4 key购买 nike

我希望能够为外键设置命名策略,当前键的命名类似于FK_qsp8qs9j1wbh965jtxfbu2q2u,我想生成类似于FK_TABLE1_TABLE2的名称,我编写了以下命名策略:

@Component
public class CustomImplicitNamingStrategy extends SpringImplicitNamingStrategy {

@Override
public Identifier determineForeignKeyName(ImplicitForeignKeyNameSource source) {
Identifier userProvidedIdentifier = source.getUserProvidedIdentifier();

if (userProvidedIdentifier != null) {
return userProvidedIdentifier;
}
String foreignKey = format("FK_%s_%s", source.getTableName().getText(), source.getReferencedTableName().getText());

return Identifier.toIdentifier(foreignKey);
}

}

application.properties

spring.jpa.hibernate.naming.implicit-strategy=foo.bar.CustomImplicitNamingStrategy 

liquibase.properties

referenceUrl=hibernate:spring:foo.bar.model?dialect=org.hibernate.dialect.Oracle10gDialect&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=foo.bar.CustomImplicitNamingStrategy

pom.xml

    <plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<!-- Liquibase plugin configuration -->
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.4.1</version>
<configuration>
<referenceUrl>
hibernate:spring:foo.bar.model?dialect=org.hibernate.dialect.Oracle10gDialect&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=foo.bar.CustomImplicitNamingStrategy
</referenceUrl>
<propertyFile>${project.basedir}/src/main/resources/liquibase.properties</propertyFile>
<diffChangeLogFile>
${project.basedir}/src/main/resources/db/changelogs/${maven.build.timestamp}_changelog.xml
</diffChangeLogFile>
<changeLogFile>
${project.basedir}/src/main/resources/db/changelogs/db.changelog-master.xml
</changeLogFile>
<logging>info</logging>
</configuration>
<dependencies>
<dependency>
<groupId>org.liquibase.ext</groupId>
<artifactId>liquibase-hibernate4</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.7.3.RELEASE</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.198</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
</dependencies>
</plugin>

</plugins>

似乎无论配置如何,liquibase 都会回退到 ImplicitNamingStrategyJpaCompliantImpl 并忽略自定义配置。

最佳答案

我发现liquibase-hibernate4中没有使用命名策略的参数。包。

我的解决方案是使用libquibase-hibernate5 (5 而不是 4) 在 maven-central (3.8) 上可用的最新版本中:

<dependency>
<groupId>org.liquibase.ext</groupId>
<artifactId>liquibase-hibernate5</artifactId>
<version>3.8</version>
</dependency>

完整的插件定义在 pom.xml是:

<!-- database migration with liquibase -->
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.8.9</version>
<configuration>
<propertyFileWillOverride>true</propertyFileWillOverride>
<propertyFile>src/main/resources/db/liquibase.properties</propertyFile>
<diffChangeLogFile>src/main/resources/db/changes/${maven.build.timestamp}_changelog.xml</diffChangeLogFile>
<verbose>true</verbose>
<logging>debug</logging>
</configuration>
<dependencies>
<dependency>
<groupId>org.liquibase.ext</groupId>
<artifactId>liquibase-hibernate5</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.20.0-GA</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>de.meisterbohne.framework</groupId>
<artifactId>meisterbohne-spring-base</artifactId>
<version>1.0.3</version>
</dependency>
</dependencies>
</plugin>

此外,我升级了liquibase-core到最新版本(3.8.9),通过覆盖父pom中的Spring默认值,并在 pom.xml 中使用以下属性:

<properties>
<liquibase.version>3.8.9</liquibase.version>
</properties>

liquibase.properties您可以设置referenceUrl带有两个用于命名策略的附加参数:

  • hibernate.physical_naming_strategy
  • hibernate.implicit_naming_strategy

示例(我使用我们自己的命名策略):

referenceUrl=hibernate:spring:\
de.meisterbohne.dms,\
de.meisterbohne.framework.spring\
?dialect=org.hibernate.dialect.MySQL5InnoDBDialect\
&hibernate.physical_naming_strategy=de.meisterbohne.framework.spring.common.data.names.MeisterbohnePhysicalNamingStrategy\
&hibernate.implicit_naming_strategy=de.meisterbohne.framework.spring.common.data.names.MeisterbohneImplicitNamingStrategy

关于java - Spring liquibase自定义ImplicitNamingStrategy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56666758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com