@Override protected final JpaRepositoryImplementation<?, ?> getTargetRepository(RepositoryInformation information) { JpaRepositoryImplementation<?, ?> repository = getTargetRepository(information, entityManager); repository.setRepositoryMethodMetadata(crudMethodMetadataPostProcessor.getCrudMethodMetadata()); return repository; }
/** * Creates a new {@link JpaRepositoryFactory}. * * @param entityManager must not be {@literal null} */ public JpaRepositoryFactory(EntityManager entityManager) { Assert.notNull(entityManager, "EntityManager must not be null!"); this.entityManager = entityManager; this.extractor = PersistenceProvider.fromEntityManager(entityManager); this.crudMethodMetadataPostProcessor = new CrudMethodMetadataPostProcessor(); this.entityPathResolver = SimpleEntityPathResolver.INSTANCE; addRepositoryProxyPostProcessor(crudMethodMetadataPostProcessor); if (extractor.equals(PersistenceProvider.ECLIPSELINK)) { addQueryCreationListener(new EclipseLinkProjectionQueryCreationListener(entityManager)); } }
@Override public void addRepositoryProxyPostProcessor(RepositoryProxyPostProcessor processor) { if (crudMethodMetadataPostProcessor != null) { Assert.notNull(processor, "RepositoryProxyPostProcessor must not be null!"); super.addRepositoryProxyPostProcessor(processor); if (postProcessors == null) { this.postProcessors = new ArrayList<>(); } this.postProcessors.add(processor); } }
@Override protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) { Class<?> repositoryInterface = metadata.getRepositoryInterface(); if (QDataTablesRepository.class.isAssignableFrom(repositoryInterface)) { return QDataTablesRepositoryImpl.class; } else { return super.getRepositoryBaseClass(metadata); } } }
@Override protected RepositoryInformation getRepositoryInformation(RepositoryMetadata metadata, Class<?> customImplementationClass) { return new EntityViewAwareRepositoryInformation((EntityViewAwareRepositoryMetadata) metadata, super.getRepositoryInformation(metadata, customImplementationClass)); }
@Override public List<QueryMethod> getQueryMethods() { return super.getQueryMethods(); }
@Override protected RepositoryMetadata getRepositoryMetadata(Class<?> repositoryInterface) { return new EntityViewAwareRepositoryMetadataImpl(super.getRepositoryMetadata(repositoryInterface), evm); }
@Override protected Optional<QueryLookupStrategy> getQueryLookupStrategy(QueryLookupStrategy.Key key, EvaluationContextProvider evaluationContextProvider) { switch (key != null ? key : QueryLookupStrategy.Key.CREATE_IF_NOT_FOUND) { case CREATE: return Optional.of(new CreateQueryLookupStrategy(entityManager, extractor, cbf, evm)); case USE_DECLARED_QUERY: return Optional.of(new DelegateQueryLookupStrategy(super.getQueryLookupStrategy(key, evaluationContextProvider).get())); case CREATE_IF_NOT_FOUND: return Optional.of(new CreateIfNotFoundQueryLookupStrategy(entityManager, extractor, new CreateQueryLookupStrategy(entityManager, extractor, cbf, evm), new DelegateQueryLookupStrategy(super.getQueryLookupStrategy(QueryLookupStrategy.Key.USE_DECLARED_QUERY, evaluationContextProvider).get()))); default: throw new IllegalArgumentException(String.format("Unsupported query lookup strategy %s!", key)); } }
/** * Gets the specified JpaRepository interface. * @param em Backing EntityManager * @param repositoryClass Repository interface class * @return JPA repository interface */ public static <T> T getJpaRepository(EntityManager em, Class<T> repositoryClass) { JpaRepositoryFactory repositoryFactory = new JpaRepositoryFactory(em); return repositoryFactory.getRepository(repositoryClass); }
EntityManager em = Persistence.getEntityManagerFactory().createEntityManager(); JpaRepositoryFactory factory = new JpaRepositoryFactory(em); UserRepository repository = factory.getRepository(UserRepository.class);
@Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { super.setBeanFactory(beanFactory); this.beanFactory = beanFactory; }
@Override public void setBeanClassLoader(ClassLoader classLoader) { super.setBeanClassLoader(classLoader); this.classLoader = classLoader; }
@Override public void addRepositoryProxyPostProcessor(RepositoryProxyPostProcessor processor) { if (crudMethodMetadataPostProcessor != null) { Assert.notNull(processor, "RepositoryProxyPostProcessor must not be null!"); super.addRepositoryProxyPostProcessor(processor); if (postProcessors == null) { this.postProcessors = new ArrayList<>(); } this.postProcessors.add(processor); } }
@Override protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) { Class<?> repositoryInterface = metadata.getRepositoryInterface(); if (DataTablesRepository.class.isAssignableFrom(repositoryInterface)) { return DataTablesRepositoryImpl.class; } else { return super.getRepositoryBaseClass(metadata); } } }
@Override protected RepositoryInformation getRepositoryInformation(RepositoryMetadata metadata, RepositoryComposition.RepositoryFragments fragments) { return getRepositoryInformation(metadata, super.getRepositoryInformation(metadata, fragments)); }
@Override public List<QueryMethod> getQueryMethods() { return super.getQueryMethods(); }
@Override protected RepositoryMetadata getRepositoryMetadata(Class<?> repositoryInterface) { return new EntityViewAwareRepositoryMetadataImpl(super.getRepositoryMetadata(repositoryInterface), evm); }
@Override protected Optional<QueryLookupStrategy> getQueryLookupStrategy(QueryLookupStrategy.Key key, EvaluationContextProvider evaluationContextProvider) { switch (key != null ? key : QueryLookupStrategy.Key.CREATE_IF_NOT_FOUND) { case CREATE: return Optional.of(new CreateQueryLookupStrategy(entityManager, extractor, cbf, evm)); case USE_DECLARED_QUERY: return Optional.of(new DelegateQueryLookupStrategy(super.getQueryLookupStrategy(key, evaluationContextProvider).get())); case CREATE_IF_NOT_FOUND: return Optional.of(new CreateIfNotFoundQueryLookupStrategy(entityManager, extractor, new CreateQueryLookupStrategy(entityManager, extractor, cbf, evm), new DelegateQueryLookupStrategy(super.getQueryLookupStrategy(QueryLookupStrategy.Key.USE_DECLARED_QUERY, evaluationContextProvider).get()))); default: throw new IllegalArgumentException(String.format("Unsupported query lookup strategy %s!", key)); } }
/** * Gets the specified JpaRepository interface. * @param repositoryClass Repository interface class * @return JPA repository interface */ public <T> T getJpaRepository(Class<T> repositoryClass) { JpaRepositoryFactory repositoryFactory = new JpaRepositoryFactory(this.em); return repositoryFactory.getRepository(repositoryClass); }
@Override public <T> T getRepository(Class<T> repositoryInterface, Object customImplementation) { if (postProcessors != null && postProcessors.get(postProcessors.size() - 1) != entityViewReplacingMethodInterceptor) { addRepositoryProxyPostProcessor(entityViewReplacingMethodInterceptor); } return super.getRepository(repositoryInterface, customImplementation); }