本文整理匯總了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;
}