Skip to content

Commit

Permalink
Polishing JpaMetamodel.
Browse files Browse the repository at this point in the history
Closes: spring-projects#3555
Original Pull Request: spring-projects#3559
  • Loading branch information
arefbehboudi authored and christophstrobl committed Aug 20, 2024
1 parent a389415 commit 9d7d735
Showing 1 changed file with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

Expand All @@ -38,15 +39,16 @@
* @author Oliver Gierke
* @author Mark Paluch
* @author Sylvère Richard
* @author Aref Behboodi
*/
public class JpaMetamodel {

private static final Map<Metamodel, JpaMetamodel> CACHE = new ConcurrentHashMap<>(4);

private final Metamodel metamodel;

private Lazy<Collection<Class<?>>> managedTypes;
private Lazy<Collection<Class<?>>> jpaEmbeddables;
private final Lazy<Collection<Class<?>>> managedTypes;
private final Lazy<Collection<Class<?>>> jpaEmbeddables;

/**
* Creates a new {@link JpaMetamodel} for the given JPA {@link Metamodel}.
Expand All @@ -61,12 +63,12 @@ private JpaMetamodel(Metamodel metamodel) {

this.managedTypes = Lazy.of(() -> metamodel.getManagedTypes().stream() //
.map(ManagedType::getJavaType) //
.filter(it -> it != null) //
.filter(Objects::nonNull) //
.collect(StreamUtils.toUnmodifiableSet()));

this.jpaEmbeddables = Lazy.of(() -> metamodel.getEmbeddables().stream() //
.map(ManagedType::getJavaType)
.filter(it -> it != null)
.filter(Objects::nonNull)
.filter(it -> AnnotatedElementUtils.isAnnotated(it, Embeddable.class))
.collect(StreamUtils.toUnmodifiableSet()));
}
Expand Down Expand Up @@ -101,7 +103,7 @@ public boolean isSingleIdAttribute(Class<?> entity, String name, Class<?> attrib
return metamodel.getEntities().stream() //
.filter(it -> entity.equals(it.getJavaType())) //
.findFirst() //
.flatMap(it -> getSingularIdAttribute(it)) //
.flatMap(JpaMetamodel::getSingularIdAttribute) //
.filter(it -> it.getJavaType().equals(attributeType)) //
.map(it -> it.getName().equals(name)) //
.orElse(false);
Expand Down

0 comments on commit 9d7d735

Please sign in to comment.