可以在@Query(org.springframework.data.jpa.repository)中使用varargs的构造函数。我的意思是这样的课:
public class EntityDTO {
public EntityDTO(Long id, LocalDate... dates) {
// some code here where dates
//which are converted to List<Pair<LocalDate, LocalDate> datesList
}
}
public interface CustomRepository extends JpaRepository<Entity, Long> {
@Query("SELECT new package.path.EntityDTO(id, date1, date2, date2) FROM Entity")
List<EntityDTO> findEntityList();
}
现在出现这样的错误:
Unsatisfied dependency expressed through constructor parameter 3
Validation failed for query for method public abstract java.util.List CustomRepository.findEntityList(java.lang.Long,java.time.LocalDate,java.time.LocalDate)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException:
Unable to locate appropriate constructor on class [EntityDTO]. Expected arguments are: long, java.time.LocalDate, java.time.LocalDate, java.time.LocalDate, java.time.LocalDate
这只是查询示例,因为dto在构造函数中可以具有2到12-14之间的id和LocalDate参数,日期可以来自不同的表(实体)。这取决于查询。为此,我希望类更通用。查询比本示例中的要复杂得多,但是我对某种构造函数感兴趣,这使我有可能创建类似于我在EntityDTO构造函数中描述的内容。可以在@Query
?中使用varargs
编辑:如果JPQL没有此功能,您有什么建议吗?
恐怕那是不可能的。
Hibernate实现匹配的构造函数解析的方式,只有具有与传递到构造函数表达式中的参数数量完全相同的参数数量的构造函数才被视为潜在匹配。
一种解决方法可能是创建一个接受列表的构造函数,然后修改您的查询沿着线的东西SELECT NEW EntityDTO(id, NEW list(date1, date2, date2))
......如果只是嵌套NEW
表达了支持。不幸的是,这是一个长期存在的功能要求。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句