During integration tests, when I try to save an entity, I catch an error. However, I use posgresql and I checked the sequence created at the time of database initialization. I do not see such an error when launching the application. only at the time of the test run.
create sequence if not exists tab_seq;
create table if not exists tab
(
id bigint not null. primary key,
.....
);
....
@Testcontainers
class DateTest {
@Autowired
private ClientRepository clientRepository;
@Container
public static PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>("postgres:12.7")
.withReuse(true)
.withInitScript("sql/create_table.sql")
.withDatabaseName("db");
@DynamicPropertySource
static void configureProperties(DynamicPropertyRegistry registry) {
registry.add("source.datasource.url", postgreSQLContainer::getJdbcUrl);
registry.add("source.datasource.username", postgreSQLContainer::getUsername);
registry.add("source.datasource.password", postgreSQLContainer::getPassword);
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManager(HikariDataSource dataSource) {
LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setDataSource(dataSource);
entityManagerFactory.setPackagesToScan(
"com.client"
);
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
entityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter);
Map<String, Object> properties = new HashMap<>();
properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
entityManagerFactory.setJpaPropertyMap(properties);
return entityManagerFactory;
}
@Entity
@Table(name = "tab", schema = "public")
public class ClientEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "tab_seq")
@SequenceGenerator(name = "tab_seq", sequenceName = "tab_seq", allocationSize = 1)
@Column(name = "id")
private Long id
I noticed that for some reason, the custom properties that were defined for the container via @DynamicPropertySource do not come here, so the built-in H2 db is taken as the configuration.
@Bean
@Primary
@ConfigurationProperties(prefix = "source.datasource")
public DataSourceProperties dbProps() {
return new DataSourceProperties();
}
Caused by: org.h2.jdbc.JdbcSQLException: Sequence "TAB_SEQ" not found; SQL statement: select nextval ('tab_seq') [90036-193] at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
Who has any idea why this is happening and how to fix it?