这是我的实体课
@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
我认为您可以使用规范。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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。