Newsletter

Hibernate Named Query Introduction Tutorial

Hibernate » on Jul 14, 2011 { 6 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]

Notes:

  • 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:

Notes:

  • 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]

Notes:

  • 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 :-)

Done…!!!!!

You Might Also Like

  ::. About the Author .::

Java4s_Author
- Full Stack Java/J2EE & UI Web 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.

Comments

6 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

Name*
Mail*
Website



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

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