Skip to content

Commit

Permalink
Rename em as entityManager in SimpleJpaRepository.
Browse files Browse the repository at this point in the history
See #3120
  • Loading branch information
jeomxon authored and gregturn committed Sep 8, 2023
1 parent 0559c38 commit 4a46dbe
Showing 1 changed file with 29 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
private static final String ID_MUST_NOT_BE_NULL = "The given id must not be null";

private final JpaEntityInformation<T, ?> entityInformation;
private final EntityManager em;
private final EntityManager entityManager;
private final PersistenceProvider provider;

private @Nullable CrudMethodMetadata metadata;
Expand All @@ -112,18 +112,18 @@ public SimpleJpaRepository(JpaEntityInformation<T, ?> entityInformation, EntityM
Assert.notNull(entityManager, "EntityManager must not be null");

this.entityInformation = entityInformation;
this.em = entityManager;
this.entityManager = entityManager;
this.provider = PersistenceProvider.fromEntityManager(entityManager);
}

/**
* Creates a new {@link SimpleJpaRepository} to manage objects of the given domain type.
*
* @param domainClass must not be {@literal null}.
* @param em must not be {@literal null}.
* @param entityManager must not be {@literal null}.
*/
public SimpleJpaRepository(Class<T> domainClass, EntityManager em) {
this(JpaEntityInformationSupport.getEntityInformation(domainClass, em), em);
public SimpleJpaRepository(Class<T> domainClass, EntityManager entityManager) {
this(JpaEntityInformationSupport.getEntityInformation(domainClass, entityManager), entityManager);
}

/**
Expand Down Expand Up @@ -183,14 +183,14 @@ public void delete(T entity) {

Class<?> type = ProxyUtils.getUserClass(entity);

T existing = (T) em.find(type, entityInformation.getId(entity));
T existing = (T) entityManager.find(type, entityInformation.getId(entity));

// if the entity to be deleted doesn't exist, delete is a NOOP
if (existing == null) {
return;
}

em.remove(em.contains(entity) ? entity : em.merge(entity));
entityManager.remove(entityManager.contains(entity) ? entity : entityManager.merge(entity));
}

@Override
Expand Down Expand Up @@ -225,7 +225,7 @@ public void deleteAllByIdInBatch(Iterable<ID> ids) {
String queryString = String.format(DELETE_ALL_QUERY_BY_ID_STRING, entityInformation.getEntityName(),
entityInformation.getIdAttribute().getName());

Query query = em.createQuery(queryString);
Query query = entityManager.createQuery(queryString);

/*
* Some JPA providers require {@code ids} to be a {@link Collection} so we must convert if it's not already.
Expand Down Expand Up @@ -266,7 +266,8 @@ public void deleteAllInBatch(Iterable<T> entities) {
return;
}

applyAndBind(getQueryString(DELETE_ALL_QUERY_STRING, entityInformation.getEntityName()), entities, em)
applyAndBind(getQueryString(DELETE_ALL_QUERY_STRING, entityInformation.getEntityName()), entities,
entityManager)
.executeUpdate();
}

Expand All @@ -283,7 +284,7 @@ public void deleteAll() {
@Transactional
public void deleteAllInBatch() {

Query query = em.createQuery(getDeleteAllQueryString());
Query query = entityManager.createQuery(getDeleteAllQueryString());

applyQueryHints(query);

Expand All @@ -298,13 +299,13 @@ public Optional<T> findById(ID id) {
Class<T> domainType = getDomainClass();

if (metadata == null) {
return Optional.ofNullable(em.find(domainType, id));
return Optional.ofNullable(entityManager.find(domainType, id));
}

LockModeType type = metadata.getLockModeType();
Map<String, Object> hints = getHints();

return Optional.ofNullable(type == null ? em.find(domainType, id, hints) : em.find(domainType, id, type, hints));
return Optional.ofNullable(type == null ? entityManager.find(domainType, id, hints) : entityManager.find(domainType, id, type, hints));
}

@Deprecated
Expand All @@ -327,7 +328,7 @@ public T getById(ID id) {
public T getReferenceById(ID id) {

Assert.notNull(id, ID_MUST_NOT_BE_NULL);
return em.getReference(getDomainClass(), id);
return entityManager.getReference(getDomainClass(), id);
}

@Override
Expand All @@ -344,7 +345,7 @@ public boolean existsById(ID id) {
Iterable<String> idAttributeNames = entityInformation.getIdAttributeNames();
String existsQuery = QueryUtils.getExistsQueryString(entityName, placeholder, idAttributeNames);

TypedQuery<Long> query = em.createQuery(existsQuery, Long.class);
TypedQuery<Long> query = entityManager.createQuery(existsQuery, Long.class);

applyQueryHints(query);

Expand Down Expand Up @@ -470,13 +471,13 @@ public <S extends T> long count(Example<S> example) {
public <S extends T> boolean exists(Example<S> example) {

Specification<S> spec = new ExampleSpecification<>(example, this.escapeCharacter);
CriteriaQuery<Integer> cq = this.em.getCriteriaBuilder() //
CriteriaQuery<Integer> cq = this.entityManager.getCriteriaBuilder() //
.createQuery(Integer.class) //
.select(this.em.getCriteriaBuilder().literal(1));
.select(this.entityManager.getCriteriaBuilder().literal(1));

applySpecificationToCriteria(spec, example.getProbeType(), cq);

TypedQuery<Integer> query = applyRepositoryMethodMetadata(this.em.createQuery(cq));
TypedQuery<Integer> query = applyRepositoryMethodMetadata(this.entityManager.createQuery(cq));
return query.setMaxResults(1).getResultList().size() == 1;
}

Expand Down Expand Up @@ -565,7 +566,7 @@ public <S extends T, R> R findBy(Specification<T> spec, Function<FetchableFluent
@Override
public long count() {

TypedQuery<Long> query = em.createQuery(getCountQueryString(), Long.class);
TypedQuery<Long> query = entityManager.createQuery(getCountQueryString(), Long.class);

applyQueryHintsForCount(query);

Expand All @@ -584,10 +585,10 @@ public <S extends T> S save(S entity) {
Assert.notNull(entity, "Entity must not be null");

if (entityInformation.isNew(entity)) {
em.persist(entity);
entityManager.persist(entity);
return entity;
} else {
return em.merge(entity);
return entityManager.merge(entity);
}
}

Expand Down Expand Up @@ -629,7 +630,7 @@ public <S extends T> List<S> saveAllAndFlush(Iterable<S> entities) {
@Transactional
@Override
public void flush() {
em.flush();
entityManager.flush();
}

/**
Expand Down Expand Up @@ -712,7 +713,7 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Sort sort) {
*/
protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec, Class<S> domainClass, Sort sort) {

CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<S> query = builder.createQuery(domainClass);

Root<S> root = applySpecificationToCriteria(spec, domainClass, query);
Expand All @@ -722,7 +723,7 @@ protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec,
query.orderBy(toOrders(sort, root, builder));
}

return applyRepositoryMethodMetadata(em.createQuery(query));
return applyRepositoryMethodMetadata(entityManager.createQuery(query));
}

/**
Expand All @@ -744,7 +745,7 @@ protected TypedQuery<Long> getCountQuery(@Nullable Specification<T> spec) {
*/
protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S> spec, Class<S> domainClass) {

CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Long> query = builder.createQuery(Long.class);

Root<S> root = applySpecificationToCriteria(spec, domainClass, query);
Expand All @@ -758,7 +759,7 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
// Remove all Orders the Specifications might have applied
query.orderBy(Collections.emptyList());

return applyRepositoryMethodMetadataForCount(em.createQuery(query));
return applyRepositoryMethodMetadataForCount(entityManager.createQuery(query));
}

/**
Expand Down Expand Up @@ -795,7 +796,7 @@ private <S, U extends T> Root<U> applySpecificationToCriteria(@Nullable Specific
return root;
}

CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
Predicate predicate = spec.toPredicate(root, query, builder);

if (predicate != null) {
Expand Down Expand Up @@ -825,7 +826,7 @@ private void applyQueryHints(Query query) {
return;
}

getQueryHints().withFetchGraphs(em).forEach(query::setHint);
getQueryHints().withFetchGraphs(entityManager).forEach(query::setHint);
applyComment(metadata, query::setHint);
}

Expand Down Expand Up @@ -854,7 +855,7 @@ private Map<String, Object> getHints() {

Map<String, Object> hints = new HashMap<>();

getQueryHints().withFetchGraphs(em).forEach(hints::put);
getQueryHints().withFetchGraphs(entityManager).forEach(hints::put);

if (metadata != null) {
applyComment(metadata, hints::put);
Expand Down

0 comments on commit 4a46dbe

Please sign in to comment.