Mapping result of a native SQL query to Grails domain class

后端 未结 3 1705
长情又很酷
长情又很酷 2020-12-14 09:46

Is it possible to map the result of a native SQL query to a collection of Grails domain class instances?

3条回答
  •  再見小時候
    2020-12-14 10:14

    import com.acme.domain.*
    
    def sessionFactory
    sessionFactory = ctx.sessionFactory  // this only necessary if your are working with the Grails console/shell
    def session = sessionFactory.currentSession 
    
    def query = session.createSQLQuery("select f.* from Foo where f.id = :filter)) order by f.name");
    query.addEntity(com.acme.domain.Foo.class); // this defines the result type of the query
    query.setInteger("filter", 88);
    query.list()*.name;
    

提交回复
热议问题