Olav Grønås Gjerde

profile image
Full stack system architect with passion for Unix, Java, Python and databases.
Twitter @olavgg
3 years ago

Native hibernate query in Grails

Here is a simple example of how to do a native database query with Hibernate

@Service(Sensor)
abstract class SensorService implements ISensorService {

    SessionFactory sessionFactory

    Sensor findByTag(String tag){
        Session session = sessionFactory.currentSession
        String sql = """
                "select * from sensor where tag = :tag
        """
        Query query = session.createNativeQuery(sql, Sensor)
        query.setParameter("tag", tag)
        return query.getSingleResult()
    }

}