Hi (excuse my english … i’m french),
I’ve a problem with multi entityManager management in a springboot projet with hibernate 5.4.23.
I’ve created as many Persistent class as i have data base like this :
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "firstEntityManagerFactory",
transactionManagerRef = "firstTransactionManager",
basePackages = { "com.ccs.editique.dao.first.repositories" }
)
public class PersistencefirstAutoConfiguration {
@Primary
@Bean(name = "firstDatasource")
@ConfigurationProperties(prefix = "spring.datasource.first")
public DataSource firstDataSource() {
return DataSourceBuilder.create().build();
}
@PersistenceContext(unitName = "firstPersistence")
@Primary
@Bean(name = "firstEntityManagerFactory")
// @Qualifier(value = "firstEntityManager")
public LocalContainerEntityManagerFactoryBean
entityManagerFactory (
EntityManagerFactoryBuilder builder,
@Qualifier("firstDatasource") DataSource firstDataSource
) {
return builder
.dataSource(firstDataSource)
.packages("com.dao.first.entities")
.persistenceUnit("firstPersistence")
.build();
}
@Primary
@Bean(name = "firstTransactionManager")
public PlatformTransactionManager firstTransactionManager (
@Qualifier("firstEntityManagerFactory") EntityManagerFactory
firstEntityManagerFactory
) {
return new JpaTransactionManager(firstEntityManagerFactory);
}
@Primary
@Bean(name = "jdbcTemplate")
public JdbcTemplate jdbcTemplate(@Qualifier("firstDatasource") DataSource firstDataSource) {
return new JdbcTemplate(firstDataSource);
}
}
The entitymanager base has the @Primary tag but not the other ones.
Everything works fine but ….
In front I want to use the bootstrap table api, and I need to get the column name to create my table. (the nativequery is totally random and the bean are not mapped)
To do so i use the NativeQueryImpl class (I know it’s deprecated but it’s still working and there is nothing else) :
@PersistenceContext(unitName = "firstPersistence")
protected EntityManager firstEntityManager;
...
Query query = firstEntityManager.createNativeQuery(requestQuery);
query.unwrap(NativeQueryImpl.class).setResultTransformer(AliasToEntityOrderedMapResultTransformer.INSTANCE);
ligneRequestListe = (List<LinkedHashMap<String, Object>>) query.getResultList();
NativeQueryImpl nativeQuery = (NativeQueryImpl) query;
nativeQuery.setResultTransformer(AliasToEntityOrderedMapResultTransformer.INSTANCE);
If the entitymanager used is the first one and has the @Primary tag, its’ working just fine but if i use another entitymanager with no @Primary tag, i get an exception :
“java.lang.ClassCastException: com.sun.proxy.$Proxy168 cannot be cast to org.hibernate.query.internal.NativeQueryImpl”
Any help / advise would be appreciated.
Tidop is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.