In hibernate we can define named queries in the mapping document. But we need to always Remember to use a CDATA section. if query contains characters that could be interpreted as markup.
So we can use as we given below:
<query name="ByNameAndMaximumWeight"><![CDATA[from r4r.DomesticStudent as student
where student.name = ?
and student.weight > ?
] ]></query>
Parameter binding and executing is done programatically:
Query q = session.getNamedQuery("ByNameAndMaximumWeight");
q.setString(0, name);
q.setInt(1, minWeight);
List cats = q.list();
The actual program code is independent of the query language that is used. You can also define native SQL queries in metadata, or migrate existing queries to Hibernate by placing them in mapping files.
while a query declaration inside a <class> element is made unique automatically by prepending the fully qualified name of the class. Also note that a query declaration inside a <hibernate-mapping> element requires a global unique name for the query,
For example: r4r.Student.ByNameAndMaximumWeight.