Named Query In Hibernate

Q. What is named query in hibernate? Example of the named query using annotation?

Steps to run below example –

Step 1 – Create a java project(Since we are not using maven for jar/library, need to download the jar and add manually in the project)

Step 2 – Download hibernate jar and add it to project( right-click on project/properties/java build path/libraries/add external jars/ browse and add).

Step 3 – define the below classes and run the main class(Don’t forget to change the username and password in hibernate.cfg.xml for the database, that you have given during installation of Oracle in your machine ).

Let’s see a simple example for the named query using annotation.

Book.java – 

hibernate.cfg.xml – 

NamedQueryExample.java  – 

When you run this program expected output is – 

Top