I'm facing a huge problem when I'm trying to use multiple datasources on spring-boot. My problem is because I'm using spring batch and I don't have enough privilegies to create the METADATA tables from spring-batch on my production database so I need to use for example a H2 to create these tables, but when I'm trying to load in my model a field with a relationship as @OneToMany in my job processor I'm receiving a LazyInitializationException
spring-boot version 2.3.4.RELEASE
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Conta> accounts = new ArrayList<>();
My processor
@Slf4j
public class AccountItemProcessor implements ItemProcessor<AccountMaster, List<AccountBatch>> {
@Autowired
private ContaBatchConfigProperties contaBatchConfigProperties;
@Override
public List<AccountBatch> process(AccountMaster accountMaster) throws Exception {
List<AccountBatch> accountBatchList = new ArrayList<>();
for (Accounts obj: accountMaster.getAccounts()) {
accountBatchList.add(ContaBatch.of(obj, contaBatchConfigProperties));
accountBatchList.add(ContaBatch.of(accountMaster, contaBatchConfigProperties, obj));
}
return accountBatchList;
}
}
Error:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: br.com.example.AccountMaster.accounts, could not initialize proxy - no Session
I created the batch datasource as the primary
@Bean(name="batchDataSource")
@Primary
@ConfigurationProperties(prefix = "spring.batch.datasource")
public DataSource batchDataSource(){
return DataSourceBuilder.create().build();
}