下面列出了org.springframework.data.domain.Persistable#org.springframework.data.repository.core.RepositoryInformation 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected Object getTargetRepository(RepositoryInformation repositoryInformation) {
JdbcAggregateTemplate template = new JdbcAggregateTemplate(publisher, context, converter, accessStrategy);
Class<?> type = repositoryInformation.getDomainType();
RelationalPath<?> relationalPathBase = getRelationalPathBase(repositoryInformation);
ConstructorExpression<?> constructor = getConstructorExpression(type, relationalPathBase);
SimpleQuerydslJdbcRepository<?, ?> repository = new SimpleQuerydslJdbcRepository(template,
context.getRequiredPersistentEntity(
type),
sqlQueryFactory,
constructor,
relationalPathBase);
if (entityCallbacks != null) {
template.setEntityCallbacks(entityCallbacks);
}
return repository;
}
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Object getTargetRepository(RepositoryInformation metadata) {
SolrOperations operations = this.solrOperations;
if (factory != null) {
SolrTemplate template = new SolrTemplate(factory);
if (this.solrOperations.getConverter() != null) {
template.setMappingContext(this.solrOperations.getConverter().getMappingContext());
}
template.setSolrCore(SolrClientUtils.resolveSolrCoreName(metadata.getDomainType()));
addSchemaCreationFeaturesIfEnabled(template);
template.afterPropertiesSet();
operations = template;
}
SimpleSolrRepository repository = getTargetRepositoryViaReflection(metadata,
getEntityInformation(metadata.getDomainType()), operations);
repository.setEntityClass(metadata.getDomainType());
this.templateHolder.add(metadata.getDomainType(), operations);
return repository;
}
public static RepositoryInformation findRepositoryInformation(
Repositories repositories, String repository) {
RepositoryInformation ri = null;
for (Class<?> clazz : repositories) {
Optional<RepositoryInformation> candidate = repositories
.getRepositoryInformationFor(clazz);
if (candidate.isPresent() == false) {
continue;
}
if (repository.equals(repositoryPath(candidate.get()))) {
ri = candidate.get();
break;
}
}
return ri;
}
public static Object findOne(Repositories repositories, Class<?> domainObjClass,
String id) throws HttpRequestMethodNotSupportedException {
Optional<Object> domainObj = null;
RepositoryInformation ri = RepositoryUtils.findRepositoryInformation(repositories,
domainObjClass);
if (ri == null) {
throw new ResourceNotFoundException();
}
Class<?> domainObjClazz = ri.getDomainType();
Class<?> idClazz = ri.getIdType();
Optional<Method> findOneMethod = ri.getCrudMethods().getFindOneMethod();
if (!findOneMethod.isPresent()) {
throw new HttpRequestMethodNotSupportedException("fineOne");
}
Object oid = new DefaultConversionService().convert(id, idClazz);
domainObj = (Optional<Object>) ReflectionUtils.invokeMethod(findOneMethod.get(),
repositories.getRepositoryFor(domainObjClazz).get(), oid);
return domainObj.orElseThrow(ResourceNotFoundException::new);
}
public static Object save(Repositories repositories, Object domainObj)
throws HttpRequestMethodNotSupportedException {
RepositoryInformation ri = RepositoryUtils.findRepositoryInformation(repositories,
domainObj.getClass());
if (ri == null) {
throw new ResourceNotFoundException();
}
Class<?> domainObjClazz = ri.getDomainType();
if (domainObjClazz != null) {
Optional<Method> saveMethod = ri.getCrudMethods().getSaveMethod();
if (!saveMethod.isPresent()) {
throw new HttpRequestMethodNotSupportedException("save");
}
domainObj = ReflectionUtils.invokeMethod(saveMethod.get(),
repositories.getRepositoryFor(domainObjClazz).get(), domainObj);
}
return domainObj;
}
protected SimpleJpaRepository<?, ?> getTargetRepository(RepositoryInformation information,
EntityManager entityManager) {
Class<?> domainType = information.getDomainType();
if (!hasAclStrategyAnnotation(domainType)) {
return super.getTargetRepository(information, entityManager);
}
JpaEntityInformation<?, Serializable> entityInformation = getEntityInformation(domainType);
// invokes
// com.github.lothar.security.acl.jpa.repository.AclJpaRepository.AclJpaRepository(JpaEntityInformation<T,
// ?>, EntityManager, JpaSpecProvider<T>)
SimpleJpaRepository<?, ?> repository = getTargetRepositoryViaReflection(information,
entityInformation, entityManager, jpaSpecProvider);
logger.debug("Created {}", repository);
return repository;
}
@Override
protected Object getTargetRepository(RepositoryInformation metadata) {
Class<?> domainType = metadata.getDomainType();
ElasticsearchEntityInformation<?, Serializable> entityInformation =
getEntityInformation(domainType);
if (!hasAclStrategyAnnotation(domainType)) {
return getTargetRepositoryViaReflection(metadata, entityInformation,
elasticsearchOperations);
}
// invokes
// com.github.lothar.security.acl.elasticsearch.repository.AclElasticsearchRepository.AclNumberKeyedRepository(ElasticsearchEntityInformation<T,
// ID>, ElasticsearchOperations, AclFilterProvider)
ElasticsearchRepository<?, ?> repository = getTargetRepositoryViaReflection(metadata,
entityInformation, elasticsearchOperations, filterProvider);
logger.debug("Created {}", repository);
return repository;
}
@Override
protected Object getTargetRepository(RepositoryInformation metadata) {
Neo4jEntityInformation<?, Object> entityInformation = getEntityInformation(metadata.getDomainType());
assertIdentifierType(metadata.getIdType(), entityInformation.getIdType());
return getTargetRepositoryViaReflection(metadata, neo4jOperations, entityInformation);
}
@Override
protected Object getTargetRepository(RepositoryInformation metadata) {
Neo4jEntityInformation<?, Object> entityInformation = getEntityInformation(metadata.getDomainType());
assertIdentifierType(metadata.getIdType(), entityInformation.getIdType());
return getTargetRepositoryViaReflection(metadata, neo4jOperations, entityInformation);
}
@BeforeEach
void setup() {
metadata = mock(RepositoryInformation.class);
entityInformation = mock(Neo4jEntityInformation.class);
doReturn(entityInformation).when(neo4jRepositoryFactory).getEntityInformation(Mockito.any());
}
@BeforeEach
void setup() {
metadata = mock(RepositoryInformation.class);
entityInformation = mock(Neo4jEntityInformation.class);
doReturn(entityInformation).when(neo4jRepositoryFactory).getEntityInformation(Mockito.any());
}
@Override
protected JpaRepositoryImplementation<?, ?> getTargetRepository(RepositoryInformation information, EntityManager entityManager) {
JpaEntityInformation<?, Serializable> entityInformation = this.getEntityInformation(information.getDomainType());
Object repository = this.getTargetRepositoryViaReflection(information, new Object[]{entityInformation, entityManager});
Assert.isInstanceOf(BaseRepositoryImpl.class, repository);
return (JpaRepositoryImplementation)repository;
}
@Override
protected JpaRepositoryImplementation<?, ?> getTargetRepository(RepositoryInformation information, EntityManager entityManager) {
JpaEntityInformation<?, Serializable> entityInformation = this.getEntityInformation(information.getDomainType());
Object repository = this.getTargetRepositoryViaReflection(information, new Object[]{entityInformation, entityManager});
Assert.isInstanceOf(BaseRepositoryImpl.class, repository);
return (JpaRepositoryImplementation)repository;
}
private RelationalPathBase<?> getRelationalPathBase(RepositoryInformation repositoryInformation) {
ResolvableType entityType = ResolvableType.forClass(repositoryInformation.getRepositoryInterface())
.as(QuerydslJdbcRepository.class)
.getGeneric(0);
if (entityType.getRawClass() == null) {
throw new IllegalArgumentException("Could not resolve query class for " + repositoryInformation);
}
return getRelationalPathBase(getQueryClass(entityType.getRawClass()));
}
@Override
protected Object getTargetRepository(RepositoryInformation information) {
final EntityInformation<?, Serializable> entityInformation =
getEntityInformation(information.getDomainType());
return getTargetRepositoryViaReflection(information, entityInformation,
this.applicationContext);
}
@Test
public void getTargetRepositoryTest() {
RepositoryInformation repoInfo = mock(RepositoryInformation.class);
Mockito.<Class<?>>when(repoInfo.getRepositoryBaseClass())
.thenReturn(SimpleDatastoreRepository.class);
Mockito.<Class<?>>when(repoInfo.getDomainType()).thenReturn(TestEntity.class);
Object repo = this.datastoreRepositoryFactory.getTargetRepository(repoInfo);
assertThat(repo.getClass()).isEqualTo(SimpleDatastoreRepository.class);
}
@Test
public void getTargetRepositoryTest() {
RepositoryInformation repoInfo = mock(RepositoryInformation.class);
// @formatter:off
Mockito.<Class<?>>when(repoInfo.getRepositoryBaseClass())
.thenReturn(SimpleSpannerRepository.class);
Mockito.<Class<?>>when(repoInfo.getDomainType()).thenReturn(TestEntity.class);
// @formatter:on
Object repo = this.spannerRepositoryFactory.getTargetRepository(repoInfo);
assertThat(repo).isInstanceOf(SimpleSpannerRepository.class);
}
@ResponseBody
@RequestMapping(value = ENTITY_FINDALLLATESTVERSION_MAPPING, method = RequestMethod.GET)
public ResponseEntity<?> findAllLatestVersion(RootResourceInformation repoInfo,
PersistentEntityResourceAssembler assembler,
@PathVariable String repository)
throws ResourceNotFoundException, HttpRequestMethodNotSupportedException {
RepositoryInformation repositoryInfo = RepositoryUtils.findRepositoryInformation(repositories, repository);
Class<?> domainType = repositoryInfo.getDomainType();
List result = (List)ReflectionUtils.invokeMethod(FINDALLLATESTVERSION_METHOD, repositories.getRepositoryFor(domainType).get());
return ResponseEntity.ok(toResources(result, assembler, this.pagedResourcesAssembler, domainType, null));
}
public static String repositoryPath(RepositoryInformation info) {
Class<?> clazz = info.getRepositoryInterface();
RepositoryRestResource annotation = AnnotationUtils.findAnnotation(clazz,
RepositoryRestResource.class);
String path = annotation == null ? null : annotation.path().trim();
path = StringUtils.hasText(path) ? path : English
.plural(StringUtils.uncapitalize(info.getDomainType().getSimpleName()));
return path;
}
public static RepositoryInformation findRepositoryInformation(
Repositories repositories, Class<?> domainObjectClass) {
RepositoryInformation ri = null;
for (Class<?> clazz : repositories) {
if (clazz.equals(domainObjectClass)) {
return repositories.getRepositoryInformationFor(clazz).get();
}
}
return ri;
}
public static Object findOne(Repositories repositories, String repository, String id)
throws HttpRequestMethodNotSupportedException {
Optional<Object> domainObj = null;
RepositoryInformation ri = RepositoryUtils.findRepositoryInformation(repositories, repository);
if (ri == null) {
throw new ResourceNotFoundException();
}
Class<?> domainObjClazz = ri.getDomainType();
Class<?> idClazz = ri.getIdType();
Optional<Method> findOneMethod = ri.getCrudMethods().getFindOneMethod();
if (!findOneMethod.isPresent()) {
throw new HttpRequestMethodNotSupportedException("fineOne");
}
Object oid = new DefaultConversionService().convert(id, idClazz);
domainObj = (Optional<Object>) ReflectionUtils.invokeMethod(findOneMethod.get(),
repositories.getRepositoryFor(domainObjClazz).get(), oid);
if (null == domainObj) {
throw new ResourceNotFoundException();
}
return domainObj.orElseThrow(ResourceNotFoundException::new);
}
public static Iterable findAll(Repositories repositories, String repository)
throws HttpRequestMethodNotSupportedException {
Iterable entities = null;
RepositoryInformation ri = RepositoryUtils.findRepositoryInformation(repositories, repository);
if (ri == null) {
throw new ResourceNotFoundException();
}
Class<?> domainObjClazz = ri.getDomainType();
Class<?> idClazz = ri.getIdType();
Optional<Method> findAllMethod = ri.getCrudMethods().getFindAllMethod();
if (!findAllMethod.isPresent()) {
throw new HttpRequestMethodNotSupportedException("fineAll");
}
entities = (Iterable) ReflectionUtils.invokeMethod(findAllMethod.get(),
repositories.getRepositoryFor(domainObjClazz));
if (null == entities) {
throw new ResourceNotFoundException();
}
return entities;
}
@Override
public void postProcess(ProxyFactory factory,
RepositoryInformation repositoryInformation) {
new MybatisBasicMapperBuilder(configuration, repositoryInformation, mappingContext
.getRequiredPersistentEntity(repositoryInformation.getDomainType()))
.build();
}
public SimpleMybatisRepository(SqlSessionTemplate sqlSessionTemplate,
RepositoryInformation repositoryInformation,
MybatisEntityInformation<T, ID> entityInformation) {
super(sqlSessionTemplate);
Assert.notNull(entityInformation, "EntityInformation must not be null!");
this.namespace = repositoryInformation.getRepositoryInterface().getName();
this.entityInformation = entityInformation;
}
public MybatisBasicMapperBuilder(Configuration configuration,
RepositoryInformation repositoryInformation,
PersistentEntity<?, ?> persistentEntity) {
super(configuration, persistentEntity,
repositoryInformation.getRepositoryInterface().getName());
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
protected Object getTargetRepository(RepositoryInformation information) {
return isBaseRepository(information.getRepositoryInterface())
? (isTreeRepository(information.getRepositoryInterface())
? new TreeRepositoryImpl(getEntityInformation(information.getDomainType()), entityManager)
: new BaseRepositoryImpl<>(getEntityInformation(information.getDomainType()),
entityManager))
: super.getTargetRepository(information);
}
/** {@inheritDoc} */
@Override protected Object getTargetRepository(RepositoryInformation metadata) {
Ignite ignite = repoToIgnite.get(metadata.getRepositoryInterface());
return getTargetRepositoryViaReflection(metadata, ignite,
getRepositoryCache(metadata.getRepositoryInterface()));
}
/** {@inheritDoc} */
@Override protected Object getTargetRepository(RepositoryInformation metadata) {
Ignite ignite = repoToIgnite.get(metadata.getRepositoryInterface());
return getTargetRepositoryViaReflection(metadata, ignite,
getRepositoryCache(metadata.getRepositoryInterface()));
}
@Override
protected Object getTargetRepository(RepositoryInformation metadata) {
// RelationalEntityInformation<?, ?> entityInformation = getEntityInformation(metadata.getDomainType());
return getTargetRepositoryViaReflection(metadata);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
protected Object getTargetRepository(final RepositoryInformation metadata) {
return new SimpleArangoRepository(arangoOperations, metadata.getDomainType());
}