@带有“order by”(小写)的公式抛出java.lang.stringindexoutofboundsexception

68de4m5k  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(290)

在@formula query string处使用小写的“order by”会引发java.lang.stringindexoutofboundsexception

public class User {
       @Formula("(SELECT string_agg(_a._teamname,';') FROM (SELECT t.name as _teamname FROM team t, teammember tm WHERE tm.team=t.id AND tm.user=id order by t.name) AS _a)")
       proctected String teamNames;
    }

hibernate-core-5.4.20版本

nle07wnf

nle07wnf1#

当hibernate计算subselectqueryfragment时,检测到的索引无效:formindex>orderbyindex。解决方法可以使用order by(大写为@formula),并将其作为良好实践扩展到所有sql公式关键字:from、where、,。。。
hibernate-core-5.4.20版本

org.hibernate.engine.spi.SubselectFetch.createSubselectFetchQueryFragment(QueryParameters)

    final int fromIndex = getFromIndex( queryString );
    final int orderByIndex = queryString.lastIndexOf( "order by" );
    final String subselectQueryFragment =  orderByIndex > 0
                ? queryString.substring( fromIndex, orderByIndex )   // -> java.lang.StringIndexOutOfBoundsException 
                : queryString.substring( fromIndex );

hibernate生成querystring

select 
     (...)
     (SELECT string_agg(_a._teamname,';') FROM (SELECT t.name as _teamname FROM team t, teammember tm WHERE tm.team=t.id AND tm.user=:id order by t.name) AS _a) as formula2_2_
     from 
       teammember teammember0_ 
     where (... without order by)

相关问题