gpt4 book ai didi

java - 具有 native 查询的存储库在测试环境中失败 - postgres、jpa、spring

转载 作者:行者123 更新时间:2023-12-02 04:20:53 26 4
gpt4 key购买 nike

我已经使用测试容器为 Spring Boot 项目设置了集成测试(使用 postgresql 设置了 docker 实例)。如果我正在测试的存储库不使用 native 查询,则测试效果很好。但是,每当存储库包含 native 查询时,我都会收到以下错误:错误:关系“my_table_here”不存在。如何让我的测试配置工作以允许 native 查询?

下面是我的测试设置:

@RunWith(SpringRunner.class)
public class TestPostgresql {

@ClassRule
public static PostgreSQLContainer postgreSQLContainer = PostgresDbContainer.getInstance();

/**
* ************ REPOSITORIES ************
*/
@Autowired
NativeQueryRepository nativeQueryRepository;

@TestConfiguration
@EnableJpaAuditing
@EnableJpaRepositories(
basePackageClasses = {
NativeQueryRepository.class
})
@ComponentScan(
basePackages = {
"com.company.project.package.repository"
}
)
static class PostgresConfiguration {

/**
* ************ DATABASE SETUP ************
*/
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl(postgreSQLContainer.getJdbcUrl());
dataSource.setUsername(postgreSQLContainer.getUsername());
dataSource.setPassword(postgreSQLContainer.getPassword());
return dataSource;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new JpaVendorAdapter();
vendorAdapter.setDatabase(Database.POSTGRESQL);
vendorAdapter.setGenerateDdl(true);

LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("com.company.project");
factory.setDataSource(dataSource());
return factory;
}

@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory);
return txManager;
}
}
}

编辑:我相信这与命名策略有关?

为了获得更多上下文,这里是如何在存储库中使用 nativeQuery 的示例

@Repository
public interface NativeQueryRepository extends JpaRepository<NativeEvent, Long> {

@Modifying
@Transactional
@Query(value = "UPDATE native_event SET state = :state " +
"WHERE secondary_id = :secondaryId", nativeQuery = true)
void updateState(
@Param("state") String state,
@Param("secondaryId") String secondaryId);

}

我还尝试通过添加注释来更新 TestPostgresql 内静态类的 testProperties:

@TestPropertySource(properties = {
"spring.jpa.hibernate.naming-strategy=org.springframework.boot.orm.jpa.SpringNamingStrategy"
})

但是,收到的错误没有改变。

编辑:添加NativeEvent:

@Entity
@Table(
name = "NativeEvent",
indexes = {
@Index(name = "idx_native_event_secondary_id", columnList = "secondaryId")
}
)
@EntityListeners(AuditingEntityListener.class)
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class NativeEvent implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name="secondaryId", nullable=false)
private String secondaryId;

@Column(name="state")
private String state;
}

最佳答案

您正在执行手动配置,而不是使用运行时配置。因此,命名策略的处理方式有所不同。相反,您应该重用相同的配置,而不是编写自己的配置。

使用@SpringBootTest@DataJpaTest并且仅重新配置DataSource

使用 ApplicationContextInitializer 执行一些操作,将 JDBC 属性获取到 ApplicationContext 中。

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(initializers = {TestPostgresql.JdbcInitializer.class})
public class TestPostgresql {

@ClassRule
public static PostgreSQLContainer postgreSQLContainer = PostgresDbContainer.getInstance();

/**
* ************ REPOSITORIES ************
*/
@Autowired
NativeQueryRepository nativeQueryRepository;

static class JdbcInitializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues.of(
"spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(),
"spring.datasource.username=" + postgreSQLContainer.getUsername(),
"spring.datasource.password=" + postgreSQLContainer.getPassword()
).applyTo(configurableApplicationContext.getEnvironment());
}
}
}

这将在您的测试中重用运行时的配置。您还应该能够使用 @DataJpaTest(NativeQueryRepository.class) 来仅为 JPA 进行切片测试,而不是使用 @SpringBootTest。

关于java - 具有 native 查询的存储库在测试环境中失败 - postgres、jpa、spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56636869/

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