开发者社区> 问答> 正文

JPA和按日期进行搜索

这是我的实体课

@Data
@NoArgsConstructor
@Builder
@AllArgsConstructor
@Entity(name = "words")
public class Word {

    @Id
    private Long id;

    @ManyToOne
    private Group group;

    @ManyToOne
    private User user;

    @NotBlank
    private String englishWord;

    @NotBlank
    private String russianWord;

    private LocalDate created = LocalDate.now();

    private final LocalDate plusOneDay = created.plusDays(1);

    private final LocalDate plusTwoDays = created.plusDays(2);

    private final LocalDate plusFiveDays = created.plusDays(5);

    private final LocalDate plusTenDays = created.plusDays(10);

    private final LocalDate plusTwoWeeks = created.plusWeeks(2);

    private final LocalDate plusSixWeeks = created.plusWeeks(6);

    private final LocalDate plusThreeMonths = created.plusMonths(3);

    private final LocalDate plusSixMonths = created.plusMonths(6);

    private final LocalDate plusOneYear = created.plusYears(1);
}```
如您所见,我有几个LocalDate字段。

我需要检查,是created,OR plusOneDay,OR plusTwoWeek,etc今天的日子相匹配。如果匹配-必须从数据库获取。我可以这样写:

Set findAllByCreatedOrByPlusOneDayOrByPlusTwoDays(LocalDate date);``` 但是请求将太长,并且不起作用。还有另一种方法可以在一个日期之前检查多个字段?

问题来源:Stack Overflow

展开
收起
montos 2020-03-27 21:57:22 335 0
1 条回答
写回答
取消 提交回答
  • 我认为您可以使用规范。Doc:https : //docs.spring.io/spring-data/jpa/docs/1.7.0.RELEASE/reference/html/#specifications

    我做了一些代码,可能会有所帮助。

    import org.springframework.data.jpa.domain.Specification;
    
    import javax.persistence.criteria.CriteriaBuilder;
    import javax.persistence.criteria.CriteriaQuery;
    import javax.persistence.criteria.Predicate;
    import javax.persistence.criteria.Root;
    import java.time.LocalDate;
    
    public class WordSpecs {
    
            public static Specification<Word> isOneDateEqual(final LocalDate date){
                return new Specification<Word>() {
                    @Override
                    public Predicate toPredicate(Root<Word> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                        final Predicate created = criteriaBuilder.equal(root.get("created"), date);
                        final Predicate plusOneDay = criteriaBuilder.equal(root.get("plusOneDay"), date);
                        return criteriaBuilder.or(created, plusOneDay);
                    }
                };
    
            }
        }```
    储存库类别:
    

    import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

    @org.springframework.stereotype.Repository public interface WordRepository extends JpaRepository<Word, Long>, JpaSpecificationExecutor {

    }``` 服务等级

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.time.LocalDate;
    import java.util.List;
    
    @Service
    public class WordService {
    
        @Autowired
        private WordRepository repository;
    
        public List<Word> findMatched(final LocalDate date){
            return repository.findAll(WordSpecs.isOneDateEqual(date));
    
        }
    
    }```
    编辑:
    
    简单得多,您可能会喜欢:
    

    @Query("SELECT word FROM Word word WHERE 1 = 1 AND word.user.userId = :userId AND ( word.created = :date OR word.plus1 = :date ) " ) List findMatched(@Param("date") final LocalDate date, @Param("userId") final Long userId);

    回答来源:Stack Overflow
    2020-03-27 21:58:09
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
《开放搜索查询分析服务架构分享》 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载