Search code examples
javahibernatespringnamed-query

Spring 2.5 Hibernate 3.5 NamedQuery


I do not use HibernateTemplate, but work with getCurrentSession() in my DAO.

I would like to know how to declare Hibernate named queries in a beans.xml file (I do not use hbm.xml).

And maybe Spring has alternative means to declare Hibernate named queries?


Solution

  • You can put named queries on the entity using annotations:

    @NamedQueries({@NamedQuery(name="Entity.findAll", query="....")})
    

    Also, if using JPA, there's orm.xml, The XSD tells us that you can use:

    <named-query name="Entity.findAll">
       <query><![CDATA[SELECT e FROM Entity e]]</query>
    </named-query>