本文整理汇总了Java中org.springframework.data.jpa.repository.JpaRepository类的典型用法代码示例。如果您正苦于以下问题:Java JpaRepository类的具体用法?Java JpaRepository怎么用?Java JpaRepository使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JpaRepository类属于org.springframework.data.jpa.repository包,在下文中一共展示了JpaRepository类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parseBaseEntity
import org.springframework.data.jpa.repository.JpaRepository; //导入依赖的package包/类
/**
* Updates a BaseEntity in the database
*
* @param value data to edit
* @param fieldType type of baseentity
* @return edited base entity
* @throws JSONException the exception
*/
private BaseEntity parseBaseEntity(final JSONObject value, final Class fieldType) throws JSONException {
final JpaRepository repository = getFieldRepository(fieldType);
Optional<BaseEntity> entity = Optional.empty();
if (value.has("id") && (!value.isNull("id"))) {
final String id = value.get("id").toString();
entity = Optional.ofNullable((BaseEntity) repository.findOne(id));
}
if (!entity.isPresent()) {
try {
entity = Optional.ofNullable((BaseEntity) Class.forName(fieldType.getName()).newInstance());
} catch (InstantiationException | ClassNotFoundException | IllegalAccessException e1) {
logger.info("[FormParse] [parseBaseEntity] Failure To Create Class Instance", e1);
}
}
entity.ifPresent(e -> {
final ParameterMapParser parser = new ParameterMapParser(value);
parser.loopData((key, data) -> writeToEntity(e, key, data));
repository.saveAndFlush(e);
});
return entity.orElseGet(null);
}
示例2: getJpaRepositoryMockForGetOne
import org.springframework.data.jpa.repository.JpaRepository; //导入依赖的package包/类
/**
* Creates a simple mock for a {@link JpaRepository} that mocks the
* {@link JpaRepository#getOne(java.io.Serializable)} to return a base
* entity that only has its id set.
*
* @param <U> type of repository
* @param <T> type of repository entity
* @param repositoryClass class of repository to be created
* @param entityClass class of the entity to be created and returned by that mock
* @param id the id that will be set on the created entity
* @return
*/
static public <U extends JpaRepository<T, Long>, T extends BaseEntity> U getJpaRepositoryMockForGetOne(Class<U> repositoryClass, Class<T> entityClass, Long id) {
try {
T baseEntity = entityClass.newInstance();
baseEntity.setId(id);
U mock = mock(repositoryClass);
when(mock.getOne(id)).thenReturn(baseEntity);
return mock;
} catch (IllegalAccessException | InstantiationException e) {
throw new RuntimeException("Can't create mock for repository", e);
}
}
示例3: scanForJpaRepositories
import org.springframework.data.jpa.repository.JpaRepository; //导入依赖的package包/类
private Map<Class<?>, Set<Class<?>>> scanForJpaRepositories(final String basePackage) {
final Map<Class<?>, Set<Class<?>>> jpaRepositories = new HashMap<Class<?>, Set<Class<?>>>();
final ClassPathScanner scanner = new ClassPathScanner().withInterfacesOnly();
scanner.addIncludeFilter(new AssignableTypeFilter(JpaRepository.class));
final Set<BeanDefinition> candidateComponents = scanner.findCandidateComponents(basePackage);
for (final BeanDefinition bd : candidateComponents) {
final String beanClassName = bd.getBeanClassName();
final Class<?> repositoryClass = Reflections.classForName(beanClassName);
if (!IDao.class.isAssignableFrom(repositoryClass)) {
final Class<?>[] typeArguments = Reflections.resolveTypeArguments(repositoryClass, JpaRepository.class);
Assertions.assertThat(typeArguments.length).isEqualTo(2);
final Class<?> entity = typeArguments[0];
Set<Class<?>> set = jpaRepositories.get(entity);
if (set == null) {
set = new HashSet<Class<?>>();
jpaRepositories.put(entity, set);
}
Assertions.assertThat(set.add(repositoryClass)).isTrue();
}
}
return jpaRepositories;
}
示例4: parseCSVDatabaseIndexes
import org.springframework.data.jpa.repository.JpaRepository; //导入依赖的package包/类
/**
* Parses a comma separated value of database indexes.
* Retrieves all the items from the database and adds them to an array to save them to the object
*
* @param value CSV of indexes
* @param repositoryClass repository of entities
* @return array of entities
*/
private Collection<BaseEntity> parseCSVDatabaseIndexes(final String value, final Class repositoryClass) {
final Set<BaseEntity> elements = new HashSet<>();
final JpaRepository repository = getFieldRepository(repositoryClass);
Arrays.stream(value.split(","))
.distinct()
.filter(Objects::nonNull)
.filter(UUIDConverter::isUUID)
.forEach(id -> elements.add((BaseEntity) repository.getOne(id)));
return elements;
}
示例5: beforeMethod
import org.springframework.data.jpa.repository.JpaRepository; //导入依赖的package包/类
@BeforeMethod
@SuppressWarnings({"unchecked"})
public void beforeMethod() {
repository = mock(JpaRepository.class);
presenter = new TestPresenter(repository, new EntityUtil());
ApplicationContext applicationContext = mock(ApplicationContext.class);
testView = mock(TestView.class);
when(applicationContext.getBean(TestView.class)).thenReturn(testView);
presenter.setApplicationContext(applicationContext);
}
示例6: getTargetRepository
import org.springframework.data.jpa.repository.JpaRepository; //导入依赖的package包/类
@Override
protected <T, ID extends Serializable> JpaRepository<?, ?> getTargetRepository(
RepositoryMetadata metadata, EntityManager entityManager) {
JpaEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata
.getDomainType());
return new GenericRepositoryImpl(entityInformation, entityManager); // custom
// implementation
}
示例7: clearRepositories
import org.springframework.data.jpa.repository.JpaRepository; //导入依赖的package包/类
@After
public void clearRepositories() throws Exception {
repositories.forEach(JpaRepository::deleteAllInBatch);
}
示例8: setDao
import org.springframework.data.jpa.repository.JpaRepository; //导入依赖的package包/类
public void setDao(final JpaRepository<T, String> repository) {
this.repository = repository;
}
示例9: selectFrom
import org.springframework.data.jpa.repository.JpaRepository; //导入依赖的package包/类
public static <ENTITY, REPO extends JpaRepository<ENTITY, ?> & JpaSpecificationExecutor>
SpecificationBuilder<ENTITY> selectFrom(REPO repository) {
SpecificationBuilder<ENTITY> builder = new SpecificationBuilder<>();
builder.repository = repository;
builder.specification = new SpecificationImpl();
return builder;
}
示例10: selectDistinctFrom
import org.springframework.data.jpa.repository.JpaRepository; //导入依赖的package包/类
public static <ENTITY, REPO extends JpaRepository<ENTITY, ?> & JpaSpecificationExecutor>
SpecificationBuilder<ENTITY> selectDistinctFrom(REPO repository) {
SpecificationBuilder<ENTITY> builder = selectFrom(repository);
builder.distinct();
return builder;
}
示例11: MagicService
import org.springframework.data.jpa.repository.JpaRepository; //导入依赖的package包/类
public MagicService(JpaRepository<T, String> jpaRepository) {
this.jpaRepository = jpaRepository;
}
示例12: getRepository
import org.springframework.data.jpa.repository.JpaRepository; //导入依赖的package包/类
@Override
protected JpaRepository<HuntingClubGroup, Long> getRepository() {
return huntingClubGroupRepository;
}
示例13: getRepository
import org.springframework.data.jpa.repository.JpaRepository; //导入依赖的package包/类
@Override
protected JpaRepository<HuntingClub, Long> getRepository() {
return huntingClubRepository;
}
示例14: getRepository
import org.springframework.data.jpa.repository.JpaRepository; //导入依赖的package包/类
@Override
protected JpaRepository<HuntingClubArea, Long> getRepository() {
return huntingClubAreaRepository;
}
示例15: getRepository
import org.springframework.data.jpa.repository.JpaRepository; //导入依赖的package包/类
@Override
protected JpaRepository<GroupHuntingDay, Long> getRepository() {
return huntingDayRepository;
}