HQL Inner join on same table

余生长醉 提交于 2019-12-11 00:19:38

问题


I asked an earlier question today which was about GORM: How to fetch records in grails by max date and group at same time However, someone suggested it can be easily achieved using HQL. But using HQL I'm getting unexpected token error. When I researched this I found out that HQL doesn't allow INNER JOINS unless there is an association between two entities: HQL, left join on the same table

So, I'm lost. To begin with, I'm frustrated why such a simple query isn't supported by GORM and now with HQL my question is: How do I perform an INNER JOIN on a subset?

What I've tried:

unexpected token: ( near line 1, column 16 [ select c from ( select name, max(dateCreated) as maxTime from com.mine.Color group by name ) as t inner join Color c on c.name = t.name and c.dateCreated = t.maxTime ]

I suspected that the second instance of Color was not being detected since the package name didn't get automatically prefixed to it. So reading other answers I tried:

unexpected token: ( near line 1, column 16 [ select c from ( select name, max(dateCreated) as maxTime from com.mine.Color group by name ) as t , com.mine.Color as c on c.name = t.name and c.dateCreated = t.maxTime ]


回答1:


Here you go:

Color.executeQuery("""    
    Select c
    From Color c
    where c.dateCreated = (select max(b.dateCreated) from Color b where b.name = c.name)
    """)


来源:https://stackoverflow.com/questions/14490581/hql-inner-join-on-same-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!