|
21 | 21 | public class EntityJavaType<T> extends AbstractClassJavaType<T> { |
22 | 22 |
|
23 | 23 | public EntityJavaType(Class<T> type, MutabilityPlan<T> mutabilityPlan) { |
24 | | - super( type, mutabilityPlan , IncomparableComparator.INSTANCE ); |
| 24 | + super( type, mutabilityPlan, IncomparableComparator.INSTANCE ); |
25 | 25 | } |
26 | 26 |
|
27 | 27 | @Override |
28 | 28 | public JdbcType getRecommendedJdbcType(JdbcTypeIndicators context) { |
29 | | - throw new JdbcTypeRecommendationException( |
30 | | - "Could not determine recommended JdbcType for '" + getTypeName() + "'" |
31 | | - ); |
| 29 | + return context.getTypeConfiguration().getSessionFactory() |
| 30 | + .getMappingMetamodel() |
| 31 | + .getEntityDescriptor(getJavaTypeClass()) |
| 32 | + .getIdentifierDescriptor() |
| 33 | + .getJavaType() |
| 34 | + .getRecommendedJdbcType( context ); |
32 | 35 | } |
33 | 36 |
|
34 | 37 | @Override |
@@ -66,18 +69,31 @@ public T fromString(CharSequence string) { |
66 | 69 | ); |
67 | 70 | } |
68 | 71 |
|
69 | | - @Override |
| 72 | + @Override @SuppressWarnings("unchecked") // safe, we do check |
70 | 73 | public <X> X unwrap(T value, Class<X> type, WrapperOptions options) { |
71 | | - throw new UnsupportedOperationException( |
72 | | - "Unwrap strategy not known for this Java type: " + getTypeName() |
73 | | - ); |
| 74 | + final var id = |
| 75 | + options.getSessionFactory().getMappingMetamodel() |
| 76 | + .getEntityDescriptor( getJavaTypeClass() ) |
| 77 | + .getIdentifier( value ); |
| 78 | + if ( !type.isInstance( id ) ) { |
| 79 | + throw new IllegalArgumentException( "Id not an instance of type " + type.getName() ); |
| 80 | + } |
| 81 | + return (X) value; |
74 | 82 | } |
75 | 83 |
|
76 | | - @Override |
| 84 | + @Override @SuppressWarnings("unchecked") // safe, we do check |
77 | 85 | public <X> T wrap(X value, WrapperOptions options) { |
78 | | - throw new UnsupportedOperationException( |
79 | | - "Wrap strategy not known for this Java type: " + getTypeName() |
80 | | - ); |
| 86 | + final var persister = |
| 87 | + options.getSessionFactory().getMappingMetamodel() |
| 88 | + .getEntityDescriptor( getJavaTypeClass() ); |
| 89 | + final var idType = persister.getIdentifierType().getReturnedClass(); |
| 90 | + if ( !idType.isInstance( value ) ) { |
| 91 | + throw new IllegalArgumentException( "Not an instance of id type " + idType.getName() ); |
| 92 | + } |
| 93 | + final var entity = |
| 94 | + options.getSession() |
| 95 | + .internalLoad( persister.getEntityName(), value, false, true ); |
| 96 | + return (T) entity; |
81 | 97 | } |
82 | 98 |
|
83 | 99 | @Override |
|
0 commit comments