@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "schedule_assignees",
joinColumns = @JoinColumn(name = "schedule_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "person_id", referencedColumnName = "id"))
private List<Person> assignedTo;
服务方法:
@Override
public List<Integer> getScheduleYearsBy(Employee employee, Company company) {
Person person = personRepository.findByIdAndCompany(employee.getId(),company);
return scheduleRepository.findYearsBy(Arrays.asList(person), company);
}
存储库方法
@Query(value = "select distinct EXTRACT(YEAR FROM s.date) from Schedule s where (s.assignedTo in ?1) " +
"and (s.company = ?2) and s.archived=false")
List<Integer> findYearsBy(List<Person> person, Company company);
此存储库方法在eclipse链接中工作正常。但是在休眠状态时,其异常情况如下
org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [com.orsbv.hcs.model.Employee@6a5] did not match expected type [java.util.Collection (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [com.orsbv.hcs.model.Employee@6a5] did not match expected type [java.util.Collection (n/a)]
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:368)
有人可以建议使其在休眠状态下工作的方法吗?
答案 0 :(得分:0)
最终更改存储库方法并使用QueryDSL并断言解决此问题
@Override
public List<Integer> findYearsBy(Person employee, Company company) {
BooleanBuilder predicate = new BooleanBuilder();
predicate.and(QSchedule.schedule.company.eq(company));
predicate.and(QSchedule.schedule.archived.eq(Boolean.FALSE));
predicate.and(QSchedule.schedule.assignedTo.any().eq(employee));
return getQuerydsl()
.createQuery()
.from(QSchedule.schedule)
.where(predicate)
.select(QSchedule.schedule.date.year())
.distinct()
.fetch();
}