I need to create an Entity Manager manually to support having multiple datasources. I've realised that by creating it manually (instead of autowiring it as when you have only one single datasource), I actually do need to set all the JPA properties manually too, including what are usually set with default values by Spring. This means all the JPA parameters I specified in application.yaml
and what Spring normally set default values for have now to be loaded and set manually.
Is there anyway to create an Entity Manager manually but have it automatically use all the JPA properties in application.yaml
?
Here's my code
public LocalContainerEntityManagerFactoryBean entityManager(EntityManagerFactoryBuilder builder, DataSource secondDataSource) {
Map<String, Object> props = new HashMap<>();
// Is there a way to not do this one by one for all of the default JPA properties and what I specified in application.yaml?
props.put("hibernate.physical_naming_strategy", "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy");
// props.put("prop1", "propvalue1");
// ...
return builder.dataSource(secondDataSource).packages(MyEntity.class).properties(props).build();
}