Spring Boot:使用 @DataJpaTest 和 Flyway 设置 Hibernate 命名策略

     2023-02-26     224

关键词:

【中文标题】Spring Boot:使用 @DataJpaTest 和 Flyway 设置 Hibernate 命名策略【英文标题】:Spring Boot: Setting Hibernate naming strategy with @DataJpaTest and Flyway 【发布时间】:2019-07-05 08:25:43 【问题描述】:

当我尝试在也使用 Flyway 的 Spring Boot 测试中使用 @DataJpaTest 注释时,我注意到了一个奇怪的行为。

给定以下实体类:

@Entity
public class MyEntity 

  @Id
  private String columnA;
  private String columnB;

  public String getColumnA() 
      return columnA;
  

  public void setColumnA(String columnA) 
      this.columnA = columnA;
  

  public String getColumnB() 
      return columnB;
  

  public void setColumnB(String columnB) 
      this.columnB = columnB;
  

以下 Spring 存储库:

@Repository
public interface MyEntityRepository extends JpaRepository<MyEntity, String> 

以下测试:

@RunWith(SpringRunner.class)
@DataJpaTest
public class JpaTestApplicationTests 

  @Autowired
  MyEntityRepository myEntityRepository;

  @Test
  public void canSaveAndFetch() 
      MyEntity myEntity = new MyEntity();
      myEntity.setColumnA("a");
      myEntity.setColumnB("b");

      myEntityRepository.save(myEntity);
      Optional<MyEntity> myEntityOptional = myEntityRepository.findById("a");
      Assert.assertTrue(myEntityOptional.isPresent());
  

测试本身运行良好,因为 auto-ddl 已打开且 H2 正在创建表。

但是,如果我想定义自己的架构,那么我将 Flyway 添加到 POM 并创建迁移,例如在资源/db/migration/V1__Schema.sql:

CREATE TABLE my_entity (
  column_a VARCHAR NOT NULL PRIMARY KEY,
  column_b VARCHAR
);

现在相同的测试将失败,因为 JPA 不再使用默认的 SpringPhysicalNamingStrategy。

原因:org.h2.jdbc.JdbcSQLException:找不到列“MYENTITY0_.COLUMNA”

通过设置这似乎是一个简单的修复

spring.jpa.hibernate.naming.physical-strategy = org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy

但是,无论我把它放在 application.properties 中,还是放在 @TestPropertySource 中,或者创建一个 PhysicalNamingStrategy bean... 都不会影响行为。如何在此测试上下文中告诉 JPA 我想使用将“columnA”映射到“column_a”的 SpringPhysicalNamingStrategy?

【问题讨论】:

你解决过这个问题吗? 【参考方案1】:

我遇到了类似的问题 - 此属性 (spring.jpa.hibernate.naming.physical-strategy) 被忽略(与其他 spring.jpa.hibernate 属性一起)。这是由扩展org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration 的类引起的,并从类org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration 覆盖了方法protected Map&lt;String, Object&gt; getVendorProperties()。删除该类(毕竟这不是必需的)解决了问题并允许HibernateJpaConfiguration 类应用配置文件中的所有属性。

【讨论】:

使用spring-boot对rest服务进行访问控制

】使用spring-boot对rest服务进行访问控制【英文标题】:Accesscontroltorestserviceswithspring-boot【发布时间】:2017-02-1601:32:44【问题描述】:我玩了一下弹簧启动安全性。我使用mongodb、spring-boot-starter-data-rest、spring-boot-starter-security和sprin... 查看详情

使用 Spring Boot 安全性在 Spring Boot 中的登录问题

】使用SpringBoot安全性在SpringBoot中的登录问题【英文标题】:LoginIssueinSpringBootusingspringbootsecurity【发布时间】:2018-09-1009:49:09【问题描述】:我是SpringBoot新手,我正在我的示例应用程序上尝试SpringBoot安全性,并且我正在使用Mongo... 查看详情

CharacterEncodingFilter 未使用 spring-boot 1.2.0 和 spring-boot-legacy 设置

】CharacterEncodingFilter未使用spring-boot1.2.0和spring-boot-legacy设置【英文标题】:CharacterEncodingFilternotsetwithspring-boot1.2.0andspring-boot-legacy【发布时间】:2015-02-2201:17:30【问题描述】:我在tomcat(servlet2.5)中运行传统的春季启动战。这些请... 查看详情

在 Tomcat 上使用 Spring Boot 进行身份验证

】在Tomcat上使用SpringBoot进行身份验证【英文标题】:GettingAuthenticationToWorkWithSpringBootOnTomcat【发布时间】:2014-09-1014:07:47【问题描述】:我正在尝试让spring-boot-starter-security与spring-boot-starter-web和spring-boot-starter-tomcat一起工作。我... 查看详情

如何使用 Spring-Boot 播种 Spring-Security

】如何使用Spring-Boot播种Spring-Security【英文标题】:HowdoIseedSpring-SecuritywithSpring-Boot【发布时间】:2014-08-0207:21:13【问题描述】:我有一个使用Spring-Securityspring-security-web:4.0.0.M1的spring-boot1.1.0.BUILD-SNAPSHOT项目。我想在我的H2表中植... 查看详情

Spring Boot 在使用 solrj 而不是 spring-boot-starter-data-solr 时会爆炸

】SpringBoot在使用solrj而不是spring-boot-starter-data-solr时会爆炸【英文标题】:SpringBootblowsupwhenusingsolrjbutnotspring-boot-starter-data-solr【发布时间】:2015-06-1318:22:23【问题描述】:我正在尝试将现有的Spring应用程序移植到SpringBoot。我不使... 查看详情

Spring Boot 应用程序使用 spring-boot-starter-actuator 给出“无法启动 tomcat”异常

】SpringBoot应用程序使用spring-boot-starter-actuator给出“无法启动tomcat”异常【英文标题】:Springbootapplicationgives"unabletostarttomcat"exceptionwithspring-boot-starter-actuator【发布时间】:2020-07-2717:16:20【问题描述】:我有我的spring-boot应... 查看详情

spring-boot 使用啥版本的 Jackson?

】spring-boot使用啥版本的Jackson?【英文标题】:WhatversionofJacksondoesspring-bootuse?spring-boot使用什么版本的Jackson?【发布时间】:2018-05-2123:11:27【问题描述】:我正在努力确保以安全的方式使用spring-boot和Jackson。Jackson的某些版本中... 查看详情

使用 JUnit 5 的 spring-boot-starter-test

】使用JUnit5的spring-boot-starter-test【英文标题】:spring-boot-starter-testwithJUnit5【发布时间】:2019-03-2213:46:28【问题描述】:从2.0.6开始使用spring-boot-starter-test会引入JUnit4依赖项。如何使用spring-boot-starter-test(通过Gradle),但使用JUnit5... 查看详情

使用 spring-boot OAuth2 服务器保护的 Spring-boot 应用程序

】使用spring-bootOAuth2服务器保护的Spring-boot应用程序【英文标题】:Spring-bootapplicationsecuredwithaspring-bootOAuth2server【发布时间】:2015-11-3016:38:21【问题描述】:我正在学习如何使用Spring-boot保护应用程序以及如何通过本教程设置OAuth2... 查看详情

在 spring-boot 项目中使用 spring mvc xml 项目

】在spring-boot项目中使用springmvcxml项目【英文标题】:Usespringmvcxmlprojectinsidespring-bootproject【发布时间】:2016-08-1308:42:55【问题描述】:我创建了这个测试项目,由2个项目组成:一个使用spring-boot,一个使用spring-mvc。它们中的每... 查看详情

使用 spring boot 和 spring-boot-maven-plugin 生成战争时排除 application.properties

】使用springboot和spring-boot-maven-plugin生成战争时排除application.properties【英文标题】:Excludeapplication.propertieswhengeneratingwarusingspringbootandspring-boot-maven-plugin【发布时间】:2014-12-3120:51:54【问题描述】:我正在使用SpringBoot开发一个Web... 查看详情

Spring-boot & hibernate,使用事务

】Spring-boot&hibernate,使用事务【英文标题】:Spring-boot&hibernate,usingtransaction【发布时间】:2014-11-2020:36:47【问题描述】:我正在尝试使用spring-boot和休眠。当我使用存储库时它工作得很好,但我试图让一个Hibernate会话来创建... 查看详情

不使用spring-boot-starter-parent

参考技术A如果想不使用spring-boot-starter-parent,可以这样配置:(1)配置使用spring-boot-dependencies注意:一定要知名是pomimport,否则会有问题。(2)配置spring-boot-maven-plugin注意:(1)如果不使用:maven-compiler-plugin,那么a)从App.main运行不... 查看详情

spring-boot如何使用两个DataSource

】spring-boot如何使用两个DataSource【英文标题】:Howusespring-bootTwoDataSources【发布时间】:2016-04-0803:44:15【问题描述】:我尝试在springboot中使用两个数据源,而不是像教程一样配置:http://docs.spring.io/spring-boot/docs/1.3.1.RELEASE/reference/h... 查看详情

如何在 Spring Boot 中使用 @Transactional 注解

】如何在SpringBoot中使用@Transactional注解【英文标题】:Howtouse@TransactionalannotationinSpringboot【发布时间】:2019-09-2014:57:56【问题描述】:我正在做一个spring-boot项目。在我的项目中使用@Transactional注解之前,我有两个问题在spring-boot... 查看详情

Apache Ignite 使用 Spring-Boot 加载两次?

】ApacheIgnite使用Spring-Boot加载两次?【英文标题】:ApacheIgniteLoadingTwicewithSpring-Boot?【发布时间】:2016-02-0121:00:54【问题描述】:我正在使用Spring-Boot1.2.7.RELEASE和ApacheIgnite1.4.0进行原型设计,发现有些奇怪,可能只是一个日志配置... 查看详情

如何使用spring boot jwt注销

】如何使用springbootjwt注销【英文标题】:Howcanlogoutusingspringbootjwt【发布时间】:2020-08-1121:28:54【问题描述】:我正在使用这个例子https://dzone.com/articles/spring-boot-security-json-web-tokenjwt-hello-world用于使用jsonWeb令牌(JWT)创建springbootresta... 查看详情