@Override protected final JpaRepositoryImplementation<?, ?> getTargetRepository(RepositoryInformation information) { JpaRepositoryImplementation<?, ?> repository = getTargetRepository(information, entityManager); repository.setRepositoryMethodMetadata(crudMethodMetadataPostProcessor.getCrudMethodMetadata()); return repository; }
/** * Returns a fresh {@link JPQLQuery}. * * @param paths must not be {@literal null}. * @return the Querydsl {@link JPQLQuery}. */ protected JPQLQuery<Object> from(EntityPath<?>... paths) { return getRequiredQuerydsl().createQuery(paths); }
/** * 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}. */ public SimpleJpaRepository(Class<T> domainClass, EntityManager em) { this(JpaEntityInformationSupport.getEntityInformation(domainClass, em), em); }
protected Class<T> getDomainClass() { return entityInformation.getJavaType(); }
@SuppressWarnings({ "unchecked", "rawtypes" }) private <S extends T> EntityInformation<Object, S> getEntityInformation(S entity) { Class<?> userClass = ClassUtils.getUserClass(entity.getClass()); if (entity instanceof AbstractPersistable<?>) { return new JpaPersistableEntityInformation(userClass, entityManager.getMetamodel()); } return new JpaMetamodelEntityInformation(userClass, entityManager.getMetamodel()); } }
/** * Creates a new {@link JPQLQuery} for the given {@link Predicate}. * * @param predicate * @return the Querydsl {@link JPQLQuery}. */ protected JPQLQuery<?> createQuery(Predicate... predicate) { AbstractJPAQuery<?, ?> query = doCreateQuery(getQueryHints().withFetchGraphs(entityManager), predicate); CrudMethodMetadata metadata = getRepositoryMethodMetadata(); if (metadata == null) { return query; } LockModeType type = metadata.getLockModeType(); return type == null ? query : query.setLockMode(type); }
/** * Creates a new {@link JPQLQuery} for the given {@link Predicate}. * * @param predicate * @return the Querydsl {@link JPQLQuery}. */ protected JPQLQuery<?> createQuery(Predicate... predicate) { AbstractJPAQuery<?, ?> query = doCreateQuery(getQueryHints().withFetchGraphs(entityManager), predicate); CrudMethodMetadata metadata = getRepositoryMethodMetadata(); if (metadata == null) { return query; } LockModeType type = metadata.getLockModeType(); return type == null ? query : query.setLockMode(type); }
@Override public Optional<T> findById(ID id) { Assert.notNull(id, ID_MUST_NOT_BE_NULL); Class<T> domainType = getDomainClass(); if (metadata == null) { return Optional.ofNullable(em.find(domainType, id)); } LockModeType type = metadata.getLockModeType(); Map<String, Object> hints = getQueryHints().withFetchGraphs(em).asMap(); return Optional.ofNullable(type == null ? em.find(domainType, id, hints) : em.find(domainType, id, type, hints)); }
@Override public <S extends T> long count(Example<S> example) { return executeCountQuery(getCountQuery(new ExampleSpecification<S>(example), example.getProbeType())); }
private <S> TypedQuery<S> applyRepositoryMethodMetadata(TypedQuery<S> query) { if (metadata == null) { return query; } LockModeType type = metadata.getLockModeType(); TypedQuery<S> toReturn = type == null ? query : query.setLockMode(type); applyQueryHints(toReturn); return toReturn; }
/** * Returns a {@link RepositoryFactorySupport}. */ protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) { JpaRepositoryFactory jpaRepositoryFactory = new JpaRepositoryFactory(entityManager); jpaRepositoryFactory.setEntityPathResolver(entityPathResolver); return jpaRepositoryFactory; }
@Override public Map<String, Object> asMap() { Map<String, Object> hints = new HashMap<>(); hints.putAll(metadata.getQueryHints()); hints.putAll(getFetchGraphs()); return hints; }
/** * Returns {@link QueryHints} with the query hints based on the current {@link CrudMethodMetadata} and potential * {@link EntityGraph} information. * * @return */ private QueryHints getQueryHints() { return metadata == null ? QueryHints.NoHints.INSTANCE : DefaultQueryHints.of(entityInformation, metadata); }
@Override @SuppressWarnings("unchecked") public Class<ID> getIdType() { return (Class<ID>) idMetadata.getType(); }
@Override public String getEntityName() { return entityName != null ? entityName : super.getEntityName(); }
@Override public boolean hasCompositeId() { return !idMetadata.hasSimpleId(); }
@Override public SingularAttribute<? super T, ?> getIdAttribute() { return idMetadata.getSimpleIdAttribute(); }
@Override @SuppressWarnings("unchecked") public <T, ID> JpaEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) { return (JpaEntityInformation<T, ID>) JpaEntityInformationSupport.getEntityInformation(domainClass, entityManager); }
/** * Returns a {@link JPQLQuery} for the given {@link EntityPath}. * * @param path must not be {@literal null}. * @return */ protected <T> JPQLQuery<T> from(EntityPath<T> path) { return getRequiredQuerydsl().createQuery(path).select(path); }
/** * Returns {@link QueryHints} with the query hints based on the current {@link CrudMethodMetadata} and potential * {@link EntityGraph} information. * * @return */ protected QueryHints getQueryHints() { return metadata == null ? NoHints.INSTANCE : DefaultQueryHints.of(entityInformation, metadata); }