groovy.lang.MissingMethodException [GrailsHttpSession.createQuery()]

时间:2015-03-02 08:25:08

标签: hibernate grails groovy hql

import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional
import java.lang.String
import org.hibernate.Query

 // grails controller logic

String hql ="SELECT state,count(team_id) FROM Obstacle where team_id = :teamId and state IN :states group BY state";

            Query t=session.createQuery(hql);

            t.setParameter("teamId",item[0]);
            t.setParameterList("states",stateList);

            List team = t.list();

以上是我正在执行的查询。

我得到的错误是:

类别:groovy.lang.MissingMethodException

消息 没有方法签名: org.codehaus.groovy.grails.web.servlet.mvc.GrailsHttpSession.createQuery()适用于参数类型:(java.lang.String)值:[SELECT state, count(team_id)FROM障碍,其中team_id =:teamId和状态IN:状态分组BY状态]

谢谢,

1 个答案:

答案 0 :(得分:3)

您面临的问题是控制器上下文中的变量session是指GrailsHttpSession,您正在寻找的是Hibernate会话。

要获得Hibernate会话,您需要使用可以注入控制器的sessionFactory,如下所示:

class MyController {
  def sessionFactory
  ...
  def someAction() {
    ...
    String hql = "..."
    def theHibernateSession = sessionFactory.currentSession
    Query t = theHibernateSession.createQuery(hql)
    ...
  }
...
}

但是,由于您使用的是Grails,您可能不会使用executeQuery或GORM中提供的任何其他方法来执行相同的操作。