本文整理匯總了Java中org.springframework.data.jpa.domain.Specification類的典型用法代碼示例。如果您正苦於以下問題:Java Specification類的具體用法?Java Specification怎麽用?Java Specification使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Specification類屬於org.springframework.data.jpa.domain包,在下文中一共展示了Specification類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: filterByKeywordAndStatus
import org.springframework.data.jpa.domain.Specification; //導入依賴的package包/類
public static Specification<Post> filterByKeywordAndStatus(
final String keyword,//
final Post.Status status) {
return (Root<Post> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
List<Predicate> predicates = new ArrayList<>();
if (StringUtils.hasText(keyword)) {
predicates.add(
cb.or(
cb.like(root.get(Post_.title), "%" + keyword + "%"),
cb.like(root.get(Post_.content), "%" + keyword + "%")
)
);
}
if (status != null) {
predicates.add(cb.equal(root.get(Post_.status), status));
}
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
};
}
示例2: applySpecificationToQueryCriteria
import org.springframework.data.jpa.domain.Specification; //導入依賴的package包/類
/**
* Spec op query doen.
* @param spec spec
* @param query query
* @param <S> query type
* @return root
*/
protected final <S> Root<T> applySpecificationToQueryCriteria(final Specification<T> spec, final CriteriaQuery<S> query) {
Assert.notNull(query, "Query mag niet null zijn.");
final Root<T> root = query.from(getDomainClass());
if (spec == null) {
return root;
}
final CriteriaBuilder builder = em.getCriteriaBuilder();
final Predicate predicate = spec.toPredicate(root, query, builder);
if (predicate != null) {
query.where(predicate);
}
return root;
}
示例3: findSentMessagesForUser
import org.springframework.data.jpa.domain.Specification; //導入依賴的package包/類
/**
* Get a specification using the specified parameters.
*
* @param user The sender's user
* @param subject The subject of the message
* @param text The text of the message
* @return The specification
*/
public static Specification<MessageEntity> findSentMessagesForUser(
final UserEntity user,
@Nullable final String subject,
@Nullable final String text
) {
return (final Root<MessageEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) -> {
final List<Predicate> predicates = new ArrayList<>();
predicates.add(cb.equal(root.get(MessageEntity_.sender), user));
if (Optional.ofNullable(subject).isPresent()) {
predicates.add(cb.like(root.get(MessageEntity_.subject), "%" + subject + "%"));
}
if (Optional.ofNullable(text).isPresent()) {
predicates.add(cb.like(root.get(MessageEntity_.text), "%" + text + "%"));
}
predicates.add(cb.isTrue(root.get(MessageEntity_.isVisibleForSender)));
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
};
}
示例4: findReceivedMessagesForUser
import org.springframework.data.jpa.domain.Specification; //導入依賴的package包/類
/**
* Get a specification using the specified parameters.
*
* @param user The recipient's user
* @param subject The subject of the message
* @param text The text of the message
* @return The specification
*/
public static Specification<MessageEntity> findReceivedMessagesForUser(
final UserEntity user,
@Nullable final String subject,
@Nullable final String text
) {
return (final Root<MessageEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) -> {
final List<Predicate> predicates = new ArrayList<>();
predicates.add(cb.equal(root.get(MessageEntity_.recipient), user));
if (Optional.ofNullable(subject).isPresent()) {
predicates.add(cb.like(root.get(MessageEntity_.subject), "%" + subject + "%"));
}
if (Optional.ofNullable(text).isPresent()) {
predicates.add(cb.like(root.get(MessageEntity_.text), "%" + text + "%"));
}
predicates.add(cb.isTrue(root.get(MessageEntity_.isVisibleForRecipient)));
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
};
}
示例5: aggregate
import org.springframework.data.jpa.domain.Specification; //導入依賴的package包/類
private <S> S aggregate(CriteriaBuilder builder, CriteriaQuery<S> query, Root<E> root, Specification<E> spec, List<Selection<?>> selectionList, LockModeType lockMode) {
if (selectionList != null) {
Predicate predicate = spec.toPredicate(root, query, builder);
if (predicate != null) {
query.where(predicate);
}
query.multiselect(selectionList);
return (S) em.createQuery(query).setLockMode(lockMode).getSingleResult();
}
return null;
}
示例6: getEntityFieldsSpec
import org.springframework.data.jpa.domain.Specification; //導入依賴的package包/類
private Specification<EventEntity> getEntityFieldsSpec(UUID tenantId, EntityId entityId, String eventType) {
return new Specification<EventEntity>() {
@Override
public Predicate toPredicate(Root<EventEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
List<Predicate> predicates = new ArrayList<Predicate>();
if (tenantId != null) {
Predicate tenantIdPredicate = criteriaBuilder.equal(root.get("tenantId"), UUIDConverter.fromTimeUUID(tenantId));
predicates.add(tenantIdPredicate);
}
if (entityId != null) {
Predicate entityTypePredicate = criteriaBuilder.equal(root.get("entityType"), entityId.getEntityType());
predicates.add(entityTypePredicate);
Predicate entityIdPredicate = criteriaBuilder.equal(root.get("entityId"), UUIDConverter.fromTimeUUID(entityId.getId()));
predicates.add(entityIdPredicate);
}
if (eventType != null) {
Predicate eventTypePredicate = criteriaBuilder.equal(root.get("eventType"), eventType);
predicates.add(eventTypePredicate);
}
return criteriaBuilder.and(predicates.toArray(new Predicate[]{}));
}
};
}
示例7: specificationToWork
import org.springframework.data.jpa.domain.Specification; //導入依賴的package包/類
private QueryPlanWork specificationToWork(Specification<T> spec, Pageable pageable) {
// Unwrap to Hibernate
final QueryImpl query = getQuery(spec, pageable).unwrap(QueryImpl.class);
final String hql = query.getQueryString();
// Create translator
final QueryTranslator queryTranslator =
new ASTQueryTranslatorFactory().createQueryTranslator("", hql, Collections.emptyMap(), em.unwrap(SessionImplementor.class).getFactory(), null);
queryTranslator.compile(Collections.emptyMap(), false);
// Get translated query and parameters
final String sql = queryTranslator.getSQLString();
final Object[] parameters = translateParameters(query, queryTranslator);
// Done
return new QueryPlanWork(sql, parameters);
}
示例8: getReplyByPage
import org.springframework.data.jpa.domain.Specification; //導入依賴的package包/類
@Override
public Page<Reply> getReplyByPage(Integer postsId, int pageNo, int length) {
Sort.Order order = new Sort.Order(Sort.Direction.ASC, "id");
Sort sort = new Sort(order);
PageRequest pageable = new PageRequest(pageNo, length, sort);
Specification<Reply> specification = new Specification<Reply>() {
@Override
public Predicate toPredicate(Root<Reply> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
Path<Integer> $posts = root.get("posts");
Predicate predicate = criteriaBuilder.and(criteriaBuilder.equal($posts, postsId));
return predicate;
}
};
Page<Reply> page = repository.findAll(specification, pageable);
return page;
}
示例9: findByPage
import org.springframework.data.jpa.domain.Specification; //導入依賴的package包/類
@Override
public Page<Reply> findByPage(Reply reply, int pageNo, int length) {
PageRequest pageable = new PageRequest(pageNo, length);
Specification<Posts> specification = new Specification<Posts>(){
@Override
public Predicate toPredicate(Root<Posts> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
Path<Integer> $id = root.get("id");
Path<String> $content = root.get("content");
Path<User> $user = root.get("user");
ArrayList<Predicate> list = new ArrayList<>();
if (reply.getId()!=null) list.add(criteriaBuilder.equal($id,reply.getId()));
if (reply.getContent()!=null) list.add(criteriaBuilder.like($content,"%" + reply.getContent() + "%"));
if (reply.getUser()!=null) list.add(criteriaBuilder.equal($user,reply.getUser()));
Predicate predicate = criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
return predicate;
}
};
Page<Reply> page = repository.findAll(specification, pageable);
return page;
}
示例10: accountExpiresBefore
import org.springframework.data.jpa.domain.Specification; //導入依賴的package包/類
/**
* All customers with an {@link Account} expiring before the given date.
*
* @param date
* @return
*/
public static Specification<Customer> accountExpiresBefore(final LocalDate date) {
return new Specification<Customer>() {
@Override
public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Root<Account> accounts = query.from(Account.class);
Path<Date> expiryDate = accounts.<Date> get("expiryDate");
Predicate customerIsAccountOwner = cb.equal(accounts.<Customer> get("customer"), root);
Predicate accountExpiryDateBefore = cb.lessThan(expiryDate, date.toDateTimeAtStartOfDay().toDate());
return cb.and(customerIsAccountOwner, accountExpiryDateBefore);
}
};
}
示例11: findAllIsFilteredBySpecificationAsExpected
import org.springframework.data.jpa.domain.Specification; //導入依賴的package包/類
@Test
public void findAllIsFilteredBySpecificationAsExpected() {
ConsoleEntity consoleEntity = new ConsoleEntity(2L);
consoleEntity.setShortName("X1");
GameEntity gameEntityToFilter = new GameEntity("Xbox Game", consoleEntity);
entityManager.persist(gameEntityToFilter);
SearchCriterion searchCriterion =
new SearchCriterion("console", "shortName", "join", "PS4");
List<SearchCriterion> searchCriteria = asList(searchCriterion);
Specification<GameEntity> specification = specificationBuilder.createSpecification(searchCriteria);
List<GameEntity> actual = repository.findAll(specification);
assertThat(actual.size()).isEqualTo(1);
}
示例12: toPredicate
import org.springframework.data.jpa.domain.Specification; //導入依賴的package包/類
@Override
public Predicate toPredicate(Root<Object> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
List<Predicate> predicates = new ArrayList<>();
for(Specification specification: specifications){
Predicate p = specification.toPredicate(root, cq, cb);
if(p!=null)
predicates.add(p);
}
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
}
示例13: findOne
import org.springframework.data.jpa.domain.Specification; //導入依賴的package包/類
@Override
public E findOne(Specification<E> spec, LockModeType lockMode) {
try {
return getQuery(spec, (Sort) null).setLockMode(lockMode).getSingleResult();
} catch (NoResultException e) {
return null;
}
}
示例14: findOne
import org.springframework.data.jpa.domain.Specification; //導入依賴的package包/類
/**
* Find one with fetched associations.
*/
private T findOne(final K id, final Map<String, JoinType> fetchedAssociations) {
final CriteriaBuilder builder = em.getCriteriaBuilder();
final CriteriaQuery<T> query = builder.createQuery(getDomainClass());
// Apply fetch
final Root<T> root = query.from(getDomainClass());
SpringUtils.getBean(FetchHelper.class).applyFetchedAssociations(fetchedAssociations, root);
// Apply specification
final Specification<T> specification = (r, q, cb) -> cb.equal(r.get("id"), id);
query.where(specification.toPredicate(root, query, builder));
query.select(root);
return em.createQuery(query).getSingleResult();
}
示例15: sum
import org.springframework.data.jpa.domain.Specification; //導入依賴的package包/類
@Override
public <S> S sum(Class<S> resultClass, Specification<E> spec, LockModeType lockMode, List<SingularAttribute<E, ? extends Number>> properties) {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<S> query = builder.createQuery(resultClass);
Root<E> root = query.from(getDomainClass());
List<Selection<?>> selectionList = Lists.newArrayList();
for (SingularAttribute<E, ? extends Number> property : properties) {
selectionList.add(builder.sum(root.get(property)));
}
return aggregate(builder, query, root, spec, selectionList, lockMode);
}