简体   繁体   English

如果我 select 超过 1 列,@Query 将不起作用

[英]@Query doesn't work if I select more than 1 column

I have a query like below that doesn't work.我有一个像下面这样的查询不起作用。
It throws IndexOutOfBoundsException: Index: 0, Size: 0它抛出IndexOutOfBoundsException: Index: 0, Size: 0
(sorry I can't post the whole stacktrace as it is in my workstation (remote)) This started to be an issue when I upgraded from Spring Boot 1.5.2.RELEASE to 1.5.22.RELEASE (抱歉,我无法发布整个堆栈跟踪,因为它在我的工作站(远程)中)当我从 Spring Boot 1.5.2.RELEASE升级到1.5.22.RELEASE时,这开始成为一个问题

@Transactional(readOnly = true)
@Query(value = "SELECT q.id as someId, q.name as someName from Quote q where q.id in (:quoteIds)")
List<Tuple> selectSomeThings(@Param("quoteIds") List<Long> quoteIds)

Now when I try to just select 1 column like so,现在,当我像这样尝试 select 1 列时,

@Transactional(readOnly = true)
@Query(value = "SELECT q.id as someId from Quote q where q.id in (:quoteIds)")
List<Tuple> selectSomeThings(@Param("quoteIds") List<Long> quoteIds)

or或者

@Transactional(readOnly = true)
@Query(value = "SELECT q.name as someName from Quote q where q.id in (:quoteIds)")
List<Tuple> selectSomeThings(@Param("quoteIds") List<Long> quoteIds)

it works.有用。 Just doesn't work when I select 2 at the same time.当我同时 select 2 时,它就不起作用了。

I would recommend to return a Bean instance from the repository methode.我建议从存储库方法返回一个 Bean 实例。

Example here:这里的例子:

    @Query("SELECT " +
           "    new com.path.to.Tuple(v.id, v.name) " +
           "FROM " +
           "    Quote v where v.id in (:quoteIds) ")
    List<Tuple> find(@Param("quoteIds") List<Long> quoteIds);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM