java - PhysicalNamingStrategy bean 仅通过配置文件实例化

标签 java spring hibernate jpa spring-boot

我在带有 Hibernate 的 Spring 应用程序中有一个自定义命名策略:

public class MyCustomPhysicalNamingStrategy implements PhysicalNamingStrategy {

  @Override
  public Identifier toPhysicalTableName(Identifier identifier, JdbcEnvironment jdbcEnvironment) {
    return Identifier.toIdentifier("my_custom_table_name");
  }

  // ...

}

The Spring documentation说我可以通过设置 spring.jpa.hibernate.naming.physical-strategy(效果很好)或像这样告诉 Hibernate 使用它:

Alternatively, if ImplicitNamingStrategy or PhysicalNamingStrategy beans are available in the application context, Hibernate will be automatically configured to use them

我需要使用第二种方法,因为我需要将一些信息从 Spring 的上下文传递到我的命名策略。但是,我无法让它工作。

我在以下配置类中创建 bean:

@Configuration
public class PersistenceConfiguration {
  @Bean
  public PhysicalNamingStrategy physicalNamingStrategy() {
    return new MyCustomPhysicalNamingStrategy();
  }
}

我在这里错过了什么?

提前致谢。

编辑:我正在使用 Spring Boot/Spring JPA 1.5.9.RELEASE,它为我提供了 Hibernate 核心 v.5.0.12.Final 和 Hibernate JPA API 2.1。

最佳答案

关于java - PhysicalNamingStrategy bean 仅通过配置文件实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49058013/

相关文章:

java - JasperReports 上的字段内字段

java - 包含库 jar 时出现 ClassNotFoundException

java - 如何创建具有相同值的新实例?

java - 如何在 Spring Rest API 中将字节数组作为参数传递

java - 如何在 Hibernate 中创建 INSERT IGNORE 查询?

java - Spring + Hibernate 的就绪配置

java - java.util.time.LocalDateTime 反序列化为 joda LocalDateTime

java - 在 Controller 上运行 JUnit 测试导致 UnsatisfiedDependencyException

java - HttpServletRequest XML 字符串参数在 POST 操作中丢失 HTML 实体

java - 我是否在组合键中正确定义了多对一?