Externalizing named queries uses in Hibernate by R4R Team

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.
Leave a Comment:
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!