Skip to content

Commit 682c92b

Browse files
committed
Prevent Hibernate basics types to be treated as DTO projection
fixes #3929 Signed-off-by: Johannes Edmeier <johannes.edmeier@steadybit.com>
1 parent 3725111 commit 682c92b

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/AbstractStringBasedJpaQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ ReturnedType getReturnedType(ResultProcessor processor) {
156156
}
157157

158158
if ((known != null && !known) || returnedJavaType.isArray() || getMetamodel().isJpaManaged(returnedJavaType)
159-
|| !returnedType.needsCustomConstruction()) {
159+
|| getMetamodel().isBasicType(returnedJavaType) || !returnedType.needsCustomConstruction()) {
160160
if (known == null) {
161161
knownProjections.put(returnedJavaType, false);
162162
}

spring-data-jpa/src/main/java/org/springframework/data/jpa/util/JpaMetamodel.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.Map;
2020
import java.util.Objects;
2121
import java.util.Optional;
22+
import java.util.Set;
23+
import java.util.HashSet;
2224
import java.util.concurrent.ConcurrentHashMap;
2325

2426
import jakarta.persistence.Embeddable;
@@ -49,6 +51,7 @@ public class JpaMetamodel {
4951

5052
private final Lazy<Collection<Class<?>>> managedTypes;
5153
private final Lazy<Collection<Class<?>>> jpaEmbeddables;
54+
private final Lazy<Collection<Class<?>>> basicTypes;
5255

5356
/**
5457
* Creates a new {@link JpaMetamodel} for the given JPA {@link Metamodel}.
@@ -71,6 +74,16 @@ private JpaMetamodel(Metamodel metamodel) {
7174
.filter(Objects::nonNull)
7275
.filter(it -> AnnotatedElementUtils.isAnnotated(it, Embeddable.class))
7376
.collect(StreamUtils.toUnmodifiableSet()));
77+
78+
this.basicTypes = Lazy.of(() -> {
79+
if (metamodel instanceof org.hibernate.metamodel.model.domain.JpaMetamodel hibernateMetamodel) {
80+
Set<Class<?>> result = new HashSet<>();
81+
hibernateMetamodel.getTypeConfiguration().getJavaTypeRegistry()
82+
.forEachDescriptor(d -> result.add(d.getJavaTypeClass()));
83+
return result;
84+
}
85+
return Set.of();
86+
});
7487
}
7588

7689
public static JpaMetamodel of(Metamodel metamodel) {
@@ -154,4 +167,9 @@ static void clear() {
154167
.filter(SingularAttribute::isId) //
155168
.findFirst();
156169
}
170+
171+
public boolean isBasicType(Class<?> type) {
172+
Assert.notNull(type, "Type must not be null");
173+
return basicTypes.get().contains(type);
174+
}
157175
}

0 commit comments

Comments
 (0)