A Named SQL query is the HQL query that is associated with a
string name and can be referenced in the application by name.
It can be used in following ways:
XMLMapping File: We can define it in XMLmapping file.
Egg.
query = "from Book s where s.author = :author”
)
})
xxxxxxxxxx
public class Fetch {
public static void main(String[] args) {
StandardServiceRegistry ssr=new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
Metadata meta=new MetadataSources(ssr).getMetadataBuilder().build();
SessionFactory factory=meta.getSessionFactoryBuilder().build();
Session session=factory.openSession();
//Hibernate Named Query
TypedQuery query = session.getNamedQuery("findEmployeeByName");
query.setParameter("name","amit");
List<Employee> employees=query.getResultList();
Iterator<Employee> itr=employees.iterator();
while(itr.hasNext()){
Employee e=itr.next();
System.out.println(e);
}
session.close();
}
}