Skip to content

Commit bdcbd99

Browse files
committed
fill in impl of methods of EntityJavaType
this is sorta useful with SQL result set mappings
1 parent 95c7bc6 commit bdcbd99

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/EntityJavaType.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@
2121
public class EntityJavaType<T> extends AbstractClassJavaType<T> {
2222

2323
public EntityJavaType(Class<T> type, MutabilityPlan<T> mutabilityPlan) {
24-
super( type, mutabilityPlan , IncomparableComparator.INSTANCE );
24+
super( type, mutabilityPlan, IncomparableComparator.INSTANCE );
2525
}
2626

2727
@Override
2828
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 );
3235
}
3336

3437
@Override
@@ -66,18 +69,31 @@ public T fromString(CharSequence string) {
6669
);
6770
}
6871

69-
@Override
72+
@Override @SuppressWarnings("unchecked") // safe, we do check
7073
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;
7482
}
7583

76-
@Override
84+
@Override @SuppressWarnings("unchecked") // safe, we do check
7785
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;
8197
}
8298

8399
@Override

0 commit comments

Comments
 (0)