Issue
In my code I need a functionality to encrypt data before saving into database and decrypt after retrieving. I tried using @Convert. But when i use that i am getting bean creation exception like below.
WARN Bean creation exception on non-lazy FactoryBean type check: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerContactNumbersDao' defined in URL [file:/C:/Users/Praveen/.jrebel/cache/com.yanasoftware.ig.commons.flights-dao/deployment/META-INF/spring/garuda-dao.xml]: Cannot create inner bean 'abstractFlightsDaoTarget$child#30674e63' of type [com.yanasoftware.flightsdao.dao.impl.GenericDaoImpl] while setting bean property 'target'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'abstractFlightsDaoTarget$child#30674e63': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flightsEntityManagerFactory' defined in URL [file:/C:/Users/Praveen/.jrebel/cache/com.yanasoftware.ig.commons.flights-dao/deployment/META-INF/spring/garuda-jpa.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: flights] Unable to build Hibernate SessionFactory
Do I have to configure attributeconverter separately or if i use @Converter
annotation is sufficient?
import org.apache.log4j.Logger;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
@Converter
public class CreditCardNumberConverter implements AttributeConverter<String, String> {
Logger LOGGER =Logger.getLogger(CreditCardNumberConverter.class);
@Override
public String convertToDatabaseColumn(String s) {
String value = CryptoLibrary.encrypt(s);
LOGGER.info("######### saving value first name : " + value);
return value;
}
@Override
public String convertToEntityAttribute(String s) {
String value = CryptoLibrary.decrypt(s);
LOGGER.info("######### getting value first name : " + value);
return value;
}
}
And in Dto class i have used following code.
@Convert(converter = CreditCardNumberConverter.class)
@Column(name = "FIRST_NAME")
public String getFirstName() {
return firstName;
}
Hibernate version 4.3.8 and jpa 2.1 Can you please tell me how to resolve the issue.
Solution
This issue is coming for audited entity classes, This is existing bug in hibernate. Alternatively we can use @Type for this kind of scenarios.
Answered By - Praveen
Answer Checked By - Candace Johnson (JavaFixing Volunteer)