Skip to content

Commit 1fc9efb

Browse files
committed
HHH-8469 - Application of JPA 2.1 AttributeConverters
1 parent b8b586a commit 1fc9efb

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

hibernate-core/src/main/java/org/hibernate/type/descriptor/converter/AttributeConverterSqlTypeDescriptorAdapter.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package org.hibernate.type.descriptor.converter;
2525

2626
import javax.persistence.AttributeConverter;
27+
import javax.persistence.PersistenceException;
2728
import java.sql.CallableStatement;
2829
import java.sql.PreparedStatement;
2930
import java.sql.ResultSet;
@@ -87,7 +88,17 @@ public <X> ValueBinder<X> getBinder(JavaTypeDescriptor<X> javaTypeDescriptor) {
8788
@Override
8889
@SuppressWarnings("unchecked")
8990
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
90-
realBinder.bind( st, converter.convertToDatabaseColumn( value ), index, options );
91+
final Object convertedValue;
92+
try {
93+
convertedValue = converter.convertToDatabaseColumn( value );
94+
}
95+
catch (PersistenceException pe) {
96+
throw pe;
97+
}
98+
catch (RuntimeException re) {
99+
throw new PersistenceException( "Error attempting to apply AttributeConverter", re );
100+
}
101+
realBinder.bind( st, convertedValue, index, options );
91102
}
92103
};
93104
}
@@ -100,21 +111,31 @@ public <X> ValueExtractor<X> getExtractor(JavaTypeDescriptor<X> javaTypeDescript
100111
final ValueExtractor realExtractor = delegate.getExtractor( intermediateJavaTypeDescriptor );
101112
return new BasicExtractor<X>( javaTypeDescriptor, this ) {
102113
@Override
103-
@SuppressWarnings("unchecked")
104114
protected X doExtract(ResultSet rs, String name, WrapperOptions options) throws SQLException {
105-
return (X) converter.convertToEntityAttribute( realExtractor.extract( rs, name, options ) );
115+
return doConversion( realExtractor.extract( rs, name, options ) );
106116
}
107117

108118
@Override
109-
@SuppressWarnings("unchecked")
110119
protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
111-
return (X) converter.convertToEntityAttribute( realExtractor.extract( statement, index, options ) );
120+
return doConversion( realExtractor.extract( statement, index, options ) );
112121
}
113122

114123
@Override
115-
@SuppressWarnings("unchecked")
116124
protected X doExtract(CallableStatement statement, String name, WrapperOptions options) throws SQLException {
117-
return (X) converter.convertToEntityAttribute( realExtractor.extract( statement, new String[] {name}, options ) );
125+
return doConversion( realExtractor.extract( statement, new String[] {name}, options ) );
126+
}
127+
128+
@SuppressWarnings("unchecked")
129+
private X doConversion(Object extractedValue) {
130+
try {
131+
return (X) converter.convertToEntityAttribute( extractedValue );
132+
}
133+
catch (PersistenceException pe) {
134+
throw pe;
135+
}
136+
catch (RuntimeException re) {
137+
throw new PersistenceException( "Error attempting to apply AttributeConverter", re );
138+
}
118139
}
119140
};
120141
}

0 commit comments

Comments
 (0)