0

Before going to actual issue let me brief what i am looking for. I am looking for encrypt and decrypt the fields inside entity. in JPA, we can use Attribute converter and achieve this. but in spring data jdbc its not supported it seems.

So, i am trying to use customconverstions feature of spring data jdbc. here i am creating one type like below

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class EncryptionDataType {
  private String value;

  @Override public String toString() {
    return value ;
  }
}

in Pojo i will use this type as field.

@Table("EMPLOYEE")
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Data
public class Employee
{
    @Column("name")
    private EncryptionDataType name;
    @Id
    private Integer id;
    @Version
    private Long version;

}

so from this i am expecting to save the 'EncryptionDataType' as normal string column in mysql for this i have created converter for read and write

@WritingConverter
public class EncryptionDataTypeWriteConverter implements Converter<EncryptionDataType, String> {
  @Override public String convert(EncryptionDataType source) {
    return source.toString()+"add";
  }
}
@ReadingConverter
public class EncryptionDataTypeReadConverter implements Converter<String, EncryptionDataType> {

  @Override public EncryptionDataType convert(String source) {
    return new EncryptionDataType(source);
  }
}

configuring these converts in configuration file.

@Configuration
public class MyConfig {

@Bean
    protected JdbcCustomConversions JdbcConversion(Dialect dialect) {
            return new JdbcCustomConversions(
                Arrays.asList(new EncryptionDataTypeReadConverter(),new EncryptionDataTypeWriteConverter()));
    }
}

This configurations seems not working. i am getting below error.

PreparedStatementCallback; bad SQL grammar [INSERT INTO `encryption_data_type` (`name`, `value`) VALUES (?, ?)]; nested exception is java.sql.SQLSyntaxErrorException: Table 'testschema.encryption_data_type' doesn't exist

seems instead of converting my encryptionDataType to string its trying to insert into new table. Please help me. am i missing anything ?

Updated configuration code:

@Configuration
@EnableJdbcRepositories(transactionManagerRef = "CustomJdbcTranasactionManager", jdbcOperationsRef = "CustomJdbcOperationsReference", repositoryFactoryBeanClass = CustomRepositoryFactoryBean.class, basePackages = {
    "com.java.testy"
})
@EnableAutoConfiguration(exclude = {
    org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class,
    org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration.class
})
public class MyConfig {

@Bean
    protected JdbcCustomConversions JdbcConversion(Dialect dialect) {
            return new JdbcCustomConversions(
                Arrays.asList(new EncryptionDataTypeReadConverter(),new EncryptionDataTypeWriteConverter()));
    }
// creating beans for datasource,JdbcOperationsReference,JdbcTranasactionManager,JdbcConverter,JdbcMappingContext,DataAccessStrategy,JdbcAggregateTemplate
}
Ravi
  • 152
  • 1
  • 11

2 Answers2

0

Make your configuration extend AbstractJdcbcConfiguration and overwrite jdbcConfiguration().

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • Instead of overriding I am defining all beans ... Reason I want these beans with qualifier since I am using multiple database – Ravi Jul 03 '22 at 13:59
0

Just updating Jens Schauder's answer (I think it's just a typo - I would comment but don't have the rep):

Make your configuration extend AbstractJdcbcConfiguration and overwrite jdbcCustomConversions() (or possibly userConverters(), if that suits the purpose).