Reputation: 47
I have an already working Jpa Repository exposing a native query method
myPrimary.datasource.jdbc-url=jdbc:sqlserver://host:1433;databaseName=dbName
myPrimary.datasource.username=user
myPrimary.datasource.password=pwd
@Configuration
@EnableJpaRepositories(
entityManagerFactoryRef = "myPrimaryEntityManagerFactory",
transactionManagerRef = "myPrimaryTransactionManager",
basePackages = {"myPrimaryPackage"}
)
@EnableTransactionManagement
public class PrimaryDaoConfig {
@Primary
@Bean(name = "myPrimaryDataSource")
@ConfigurationProperties(prefix = "myPrimary.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "myPrimaryEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("myPrimaryDataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages("myPrimaryPackage").persistenceUnit("myPrimary").build();
}
@Primary
@Bean(name = "myPrimaryTransactionManager")
public PlatformTransactionManager transactionManager(@Qualifier("myPrimaryEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
@Repository
public interface MyPrimaryRepository<T extends MyPrimaryEntity, ID extends MyPrimaryId> extends org.springframework.data.repository.Repository<T, ID> {
String MY_CUSTOM_NATIVE_QUERY = "...";
@Query(
value = MY_CUSTOM_NATIVE_QUERY,
nativeQuery = true
)
List<MyPrimaryEntity> findAll();
}
@RunWith(SpringRunner.class)
@ActiveProfiles("dev")
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class RepositoryTest {
@Autowired
private MyPrimaryRepository myPrimaryRepository;
@Test
public void findAll() {
assertNotNull(myPrimaryRepository);
List<MyPrimaryEntity> entities = myPrimaryRepository.findAll();
assertEquals(332, entities.size());
}
}
I'd like to use it in Spring IntegrationFlows.
From the docs I found, there's a way to pass an EntityManagerFactory like this
@Bean
public IntegrationFlow dbInboundFlow() throws Exception {
return IntegrationFlows
.from(Jpa
.inboundAdapter(myPrimaryEntityManagerFactory)
.nativeQuery(MyPrimaryRepository.MY_CUSTOM_NATIVE_QUERY)
.entityClass(MyPrimaryEntity.class),
e -> e.poller(Pollers.fixedDelay(10000))
)
.handle(jobLaunchingMessageHandler())
.channel("nullChannel")
.get();
}
My unit test is successful, but when trying to pass the entityMangerFactory in the IntegrationFlows, I get the
Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
error. I tried to add
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
but no luck either.
So is there a way to pass a repository instead of the entityManagerFactory ?
Upvotes: 0
Views: 211
Reputation: 121552
For now I only can answer that if you have already a JPA repository, you should use a generic method invocation MessageSource
instead of that Jpa.inboundChannelAdapter()
. The last one technically does for us whatever repository does but more in messaging manner.
The issue with dialect not clear for me: looks like you use the same. The dialect if not set explicitly is derived from the JpaVendorAdapter
. The HibernateJpaConfiguration
should do a stuff for us on the matter. Not clear if @DataJpaTest
does for us everything what we needed.
Nothing to do with Spring Integration though. I'd like to see some simple project from you to play with locally on my side.
Upvotes: 1