• +91 9723535972
  • info@interviewmaterial.com

Hibernate Interview Questions and Answers

Question - Can you tell something about Named SQL Query

Answer -

A named SQL query is an expression represented in the form of a table. Here, SQL expressions to select/retrieve rows and columns from one or more tables in one or more databases can be specified. This is like using aliases to the queries.

In hibernate, we can make use of @NameQueries and @NameQuery annotations.

@NameQueries annotation is used for defining multiple named queries.
@NameQuery annotation is used for defining a single named query.
Code Snippet: We can define Named Query as shown below

@NamedQueries(  
   {  
       @NamedQuery(  
       name = "findIBEmployeeByFullName",  
       query = "from InterviewBitEmployee e where e.fullName = :fullName"  
       )  
   }  
)  
:fullName refers to the parameter that is programmer defined and can be set using the query.setParameter method while using the named query.

Usage:

TypedQuery query = session.getNamedQuery("findIBEmployeeByFullName");    
query.setParameter("fullName","Hibernate");   
List ibEmployees = query.getResultList();
The getNamedQuery method takes the name of the named query and returns the query instance.

Comment(S)

Show all Coment

Leave a Comment




NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners