我希望能够为外键设置命名策略,当前键的命名为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
并忽略自定义项。
答案 0 :(得分:0)
我发现liquibase-hibernate4
软件包中没有使用命名策略的参数。
我的解决方案是使用maven-central(3.8)上可用的最新版本的libquibase-hibernate5
(从5代替4):
<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>
此外,我通过从父pom覆盖Spring默认值(在liquibase-core
中使用以下属性)将pom.xml
升级到了最新版本(3.8.9):
<properties>
<liquibase.version>3.8.9</liquibase.version>
</properties>
在liquibase.properties
中,您可以为命名策略设置两个附加参数来设置referenceUrl
:
示例(我使用自己的命名策略):
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