Reputation: 51
I'm testing the new JPA 2.1 Type Converters. I want to avoid NULL String values to be stored in a legacy database as they are not allowed. So I defined the following converter:
@Converter(autoApply=true)
public class CString implements AttributeConverter<String, String> {
@Override
public String convertToDatabaseColumn(String str) {
if( str == null || str.length() == 0 ) {
return " ";
} else {
return str;
}
}
@Override
public String convertToEntityAttribute(String str) {
if( str == null || str.length() == 0 || str.equals(" ") ) {
return null;
} else {
return str;
}
}
}
String properties should be converted to a space character if they are NULL, but the converter method's are not executed when the properties are NULL.
I'm trying hibernate-jpa-2.1-api (1.0.0.Final) and hibernate-entitymanager (4.3.6.Final).
Is there any JPA 2.1 compliant way to get around this?
Upvotes: 5
Views: 2800
Reputation: 1805
UPDATE: this bug has been resolved in the latest Hibernate 5.0.0.Beta1 as part of JIRA issue HHH-8697 and will be in Hibernate 4.3.9 as soon as it is released from the 4.3.9-SNAPSHOT version
If you don't want to upgrade to the beta version, you can use a workaround: take a look at JPA/Hibernate map null which explains using the getter and setter to implement your logic.
Upvotes: 2