Using a @Convert(converter = Xxx.class) has nothing to do with a specific dataSource. You should be able to use the converter in every @Entity.
Here is a working demo: multi-datasource-converter
It uses your referenced converter on multiple datasources.
Snippets
application.properties
foo.datasource.jdbcUrl=jdbc:h2:mem:foo
foo.datasource.username=sa
foo.datasource.password=
foo.datasource.driver-class-name=org.h2.Driver
bar.datasource.jdbcUrl=jdbc:h2:mem:bar
bar.datasource.username=sa
bar.datasource.password=
bar.datasource.driver-class-name=org.h2.Driver
Configuration of the datasources, select one as @Primary and point both EntityManagerFactoryBuilders to the correct packages
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "fooEntityManagerFactory",
transactionManagerRef = "fooTransactionManager",
basePackageClasses = FooRepository.class)
public class FooJpaConfiguration {
@Primary
@Bean(name = "fooDataSource")
@ConfigurationProperties(prefix = "foo.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "fooEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
EntityManagerFactoryBuilder builder, @Qualifier("fooDataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages("com.example.demo.foo").persistenceUnit("foo")....
....
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "barEntityManagerFactory",
transactionManagerRef = "barTransactionManager",
basePackageClasses = BarRepository.class )
public class BarJpaConfiguration {
@Bean(name = "barDataSource")
@ConfigurationProperties(prefix = "bar.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "barEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean barEntityManagerFactory(
EntityManagerFactoryBuilder builder, @Qualifier("barDataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages("com.example.demo.bar").persistenceUnit("bar")
And use the converter in your Entities
package com.example.demo.foo;
...
@Entity
public class Foo {
@Id
private Long id;
private String name;
@Convert(converter = StringEncryptDecryptConverter.class)
private String secret;
....
package com.example.demo.bar;
...
@Entity
public class Bar {
@Id
private Long id;
private String name;
@Convert(converter = StringEncryptDecryptConverter.class)
private String secret;