1
votes

I have created spring boot application with multiple datasources by referring link below: https://medium.com/@joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7

And added encryption at entity level by referring link below: https://github.com/sunitk/generic-jpa-converter-encrypt-decrypt

With multiple datasources configuration I am not able to get the property value in Attribute Converter. Its coming as null.

But with single datasource (Default properties) I am able to get the property value.

Please let me know how can I get property value with multiple datasource configuration instead of default single datasource properties ?

1
working demo on github, what is your problem? - Dirk Deyne

1 Answers

0
votes

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;