Hibernate Named Query Introduction Tutorial

Hibernate » on Jul 14, 2011 { 8 Comments } By Sivateja

Let us see few points, before going to see an example on Named Queries in HIbernate..

  • While executing either HQL, NativeSQL Queries if we want to execute the same queries for multiple times and in more than one client program application then we can use the Named Queries mechanism
  • In this Named Queries concept, we use some name for the query configuration, and that name will be used when ever the same query is required to execute
  • In hibernate mapping file we need to configure a query by putting some name for it and in the client application, we need to use getNamedQuery() given by session interface, for getting the Query reference and we need to execute that query by calling list()
  • If you want to create Named Query then we need to use query element in the hibernate mapping file

Syntax Of hibernate mapping file [For HQL]


<class name="---" table="---">
   <id name="---" column="---"  />
   <property name="---" column="---" length="10"/>
   <property name="---" column="---" />
   --- --- --- ---

<query name="Give Query Name">
    <![CDATA[from Product p where p.price = :java4s]]>



  • See line numbers 10,11,12, this is the new element we have to add to work with Named Queries
  • there colon (:) java4s is the label, i will pass the value into that label in the run time.., or let us see the client program logic too

Example Logic in Application:

Query qry = session.getNamedQuery("Name we given in hibernate mapping xml");
qry.setParameter("java4s",new Integer(1022));
List l = qry.list();


  • Line number 1, getting the query from hibernate mapping file to our client program
  • Line number 2, passing run time value to that query
  • Line number 3, calling list() method to execute the query


Up to now this is the case if we use HQL query in hibernate mapping file, let us see the case if we would like to use nativeSQL query

Syntax Of hibernate mapping file [For Native SQL]


<class name="---" table="---">
   <id name="---" column="---"  />
   <property name="---" column="---" length="10"/>
   <property name="---" column="---" />
   --- --- --- ---

<sql-query name="Give Query Name">
    select * from PRODUCTS



  • If we want to give HQL query in hiberante mapping file, we need to use <query/> element, but we have to use <sql-query /> element in case of Native SQL
  • See line number 11, its the normal sql command, and PRODUCTS is the table name, not the pojo class name 🙂



You Might Also Like

  ::. About the Author .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


8 Responses to “Hibernate Named Query Introduction Tutorial”
  1. Glen Mackinder says:

    I like this blog very much so much good info.

  2. jaffar says:

    Hi sir,
    Somewhere i read that there are different types of data structures in hibernate to store. one of it is: org.hibernate.mapping.Bag

    can you explain us about it?

    one more query is, What is the use of Template concept in hibernate? I need badly it.

    Thanks in advance..:-)

  3. Anand says:

    Sir please tell me the difference between CDATA and PCDATA…and why have you used CDATA in sql-query..

    Thank you Sir.

    • Gopi krishna says:

      Hi anand,

      cdata and pcdata are dtd xml file variables.
      named queary's are .dtd xml file .so we are using CDATA and PCDATA.

    • Pravash Ranjan says:

      CDATA is nothing, what we use in our day today life….Character data….

      PCDATA: is data, which can be parsable by a parser..It may not be human understandable data…

  4. Ayush says:

    Excellent Blogs . find much useful information

  5. GMRao says:

    I have a POJO class and hibernate file that POJO, and i want to write native sql query in another file for this POJO with joins, is it possible..?

  6. Nisha says:

    Could you please provide us example for Named query using annotations..!!


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.