Spring boot Spring Boot:使用@DataJpaTest和Flyway设置Hibernate命名策略
我注意到在Spring引导测试中尝试使用@DataJpaTest注释时出现了一个奇怪的行为,该测试也使用Flyway 给定以下实体类:Spring boot Spring Boot:使用@DataJpaTest和Flyway设置Hibernate命名策略,spring-boot,jpa,junit,h2,flyway,Spring Boot,Jpa,Junit,H2,Flyway,我注意到在Spring引导测试中尝试使用@DataJpaTest注释时出现了一个奇怪的行为,该测试也使用Flyway 给定以下实体类: @Entity public class MyEntity { @Id private String columnA; private String columnB; public String getColumnA() { return columnA; } public void setColumnA(String
@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> {
}
现在同样的测试将失败,因为JPA不再使用默认的SpringPhysicalNamingStrategy
原因:org.h2.jdbc.JdbcSQLException:找不到列“MYENTITY0_389;.COLUMNA”
这似乎是通过设置
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
然而,无论我将它放在application.properties中,还是放在@TestPropertySource中,或者创建一个PhysicalNamingStrategybean中。。。没有任何东西会影响这种行为。在这个测试上下文中,我如何告诉JPA我想使用SpringPhysicalNamingStrategy,它将“columnA”映射到“column_a”?我有类似的问题-这个属性(
spring.JPA.hibernate.naming.physical strategy
)被忽略(与其他spring.JPA.hibernate
属性一起)。这是由于类扩展了org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration并从classorg.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration
重写了方法protectedmap getVendorProperties()
。删除该类(这毕竟不是必需的)解决了问题,并允许HibernateJpaConfiguration
类应用配置文件中的所有属性。您解决过这个问题吗?
@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());
}
}
CREATE TABLE my_entity (
column_a VARCHAR NOT NULL PRIMARY KEY,
column_b VARCHAR
);