2

I have encrypted spring JPA Entity data using custom Attribute converter. I was working nice with single(default) postgres datasource in spring boot. Now when I implemented multiple datasources suddenly entity column encryption/decryption stopped. I have no clue why Attribute converter is not working in case of multiple datasources.

**Error:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: Error attempting to apply AttributeConverter; nested exception is javax.persistence.PersistenceException: Error attempting to apply AttributeConverter**

Sample Entity Class code:

@Entity
@Table(name="users")
@Data
public class User{

    @Id
    private String id;
    @Column(name="u_name")
    @Convert(converter = StringEncryptDecryptConverter.class)
    private String userName;
    @Column(name="u_contact")
    @Convert(converter = StringEncryptDecryptConverter.class)
    private String contact;
    @Column(name = "u_address")
    private String address;

}

StringEncryptDecryptConverter Class Code:

@Converter(autoApply = false)
public class StringEncryptDecryptConverter
extends AbstractEncryptDecryptConverter<String> {

    /**
     * Default constructor initializes with an instance of the
     * {@link CipherMaker} crypto class to get a {@link javax.crypto.Cipher}
     * instance
     */
    public StringEncryptDecryptConverter() {
        this(new CipherMaker());
    }

    /**
     * Constructor
     * 
     * @param cipherMaker
     */
    public StringEncryptDecryptConverter(CipherMaker cipherMaker) {
        super(cipherMaker);
    }

    @Override
    boolean isNotNullOrEmpty(String attribute) {
        return isNotEmpty(attribute);
    }

    @Override
    String convertStringToEntityAttribute(String dbData) {
        return dbData;
    }

    @Override
    String convertEntityAttributeToString(String attribute) {
        return attribute;
    }
}

application.properties :

spring.datasource.jdbcUrl=jdbc:postgresql://127.0.0.1:5432/test_Db
spring.datasource.username=postgres
spring.datasource.password=root

spring.another-datasource.jdbcUrl=jdbc:postgresql://127.0.0.1:5432/another_db
spring.another-datasource.username=postgres
spring.another-datasource.password=root*
1
  • 1
    Did you get any solution ? Commented Aug 10, 2020 at 11:12

1 Answer 1

1

When defining multiple datasources, you assign packages to the respective EntityManagers by calling LocalContainerEntityManagerFactoryBean.setPackagesToScan(String... packagesToScan). What's less obvous is that you define which packages Hibernate should scan in general, not just for entities. What's not in these packages doesn't exist for this EntityManager. You need to add your converter-packages to this list for every EntityManager that should use this converter.

In hindsight, it's pretty obvious but wasn't easy to figure out.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.