Newsletter

Mapping And Configuration Files In Hibernate

Hibernate » on May 4, 2011 { 60 Comments } By Sivateja

Mapping and Configuration are very familiar keywords we used to here in the hibernate, every hibernate program must need these 2 xml files.

Mapping:

  • Mapping file is the heart of hibernate application.
  • Every ORM tool needs this mapping, mapping is the mechanism of placing an object properties into column’s of a table.
  • Mapping can be given to an ORM tool either in the form of an XML or in the form of the annotations.
  • The mapping file contains mapping from a pojo class name to a table name and pojo class variable names to table column names.
  • While writing an hibernate application, we can construct one or more mapping files, mean a hibernate application can contain any number of  mapping files.

generally an object contains 3 properties like

  • Identity (Object Name)
  • State (Object values)
  • Behavior (Object Methods)

But while storing an object into the database, we need to store only the values(State) right ? but how to avoid identity, behavior.. its not possible. In order to inform what value of an object has to be stored in what column of the table, will be taking care by the mapping,  actually mapping can be done using 2 ways,

  • XML
  • Annotations.

Actually annotations are introduced into java from JDK 1.5.

Syntax Of Mapping xml:

<hibernate-mapping>

<class name="POJO class name" table="table name in database">
<id name="variable name" column="column name in database" type="java/hibernate type" />
<property name="variable1 name" column="column name in database" type="java/hibernate type" />
<property name="variable2 name" column="column name in database" type="java/hibernate type" />
</class>

</hibernate-mapping>

Configuration:

Configuration is the file loaded into an hibernate application when working with hibernate, this configuration file contains 3 types of information..

  • Connection Properties
  • Hibernate Properties
  • Mapping file name(s)

We must create one configuration file for each database we are going to use, suppose if we want to connect with 2 databases, like Oracle, MySql, then we must create 2 configuration files.

No. of databases we are using  = That many number of configuration files

We can write this configuration in 2 ways…

  • xml
  • By writing Properties file.  We don’t have annotations here, actually in hibernate 1, 2.x we defined this configuration file by writing .properties file, but from 3.x xml came into picture.

so, finally

Mapping –> xml, annotations
Configuration –> xml, .properties (old style)

 

Syntax Of Configuration xml:

<hibernate-configuration>
<session-factory>

<!-- Related to the connection START -->
<property name="connection.driver_class">Driver Class Name </property>
<property name="connection.url">URL </property>
<property name="connection.user">user </property>
<property name="connection.password">password</property>
<!-- Related to the connection END -->

<!-- Related to hibernate properties START -->
<property name="show_sql">true/false</property>
<property name="dialet">Database dialet class</property>
<property name="hbm2ddl.auto">create/update or what ever</property>
<!-- Related to hibernate properties END-->

<!-- Related to mapping START-->
<mapping resource="hbm file 1 name .xml" / >
<mapping resource="hbm file 2 name .xml" / >
<!-- Related to the mapping END -->

</session-factory>
</hibernate-configuration>

But XML files are always recommended to work.

​​

You Might Also Like

  ::. About the Author .::

Java4s_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.

Comments

60 Responses to “Mapping And Configuration Files In Hibernate”
  1. very nice tutorials…….
    Thanks a lot…

  2. Really a simple and nice tutorial to learn in easily Hibernate. Thanks a lot. I am going to bookmark it.

  3. Excellent way of explaining..quite easy and understandable… hope I am going to learn hibernate this time 🙂

  4. satish says:

    can u plz explain the steps of execution?i mean is hibernate.cfg.xml file is read first or xxx.hbm.xml file is read first?

  5. Nice One I appreciate this work

  6. Akbar says:

    simple and easily understandable to every one …
    thank u for giving such a great tutorial….

  7. TAUFIK says:

    Very Useful Site And feelling like Very Easy to Learn

  8. Rajesh says:

    Thanks Bro. It made my life easy.
    Its Really a Good job.

  9. samba says:

    thanks to providing this kind of easy learning process.according to as you said suppose if we work with two different database we need to take two different configuration file.why we cant maintain in single file with two session factory configuration in single file.any how thanks to provide best tutorial.

  10. Sireesha says:

    Which version of hibernate we should download to run through the trail examples mentioned in the website

  11. amit jain says:

    Very Nice….. I appreciate this work…
    thank you very much..

  12. Sonu says:

    Superb….No words !!

  13. Venkat says:

    Thanks for your wonderful site.
    As earlier you mentioned in hibernate JDBC problems
    ” here as a programmer we must close the connection, or we may get a chance to get our of connections message…!” but in hibernate also we are closing Database connections..So even this would be problem in hibernate also… can you clarify me?
    thanks in advance

    • POORNACHANDER BACHU says:

      Hibernate is the responsible for opening and closing the connection. we no need to open and close connections.

  14. Shakthi says:

    tutorials are very useful…

  15. Chetan says:

    nice tutorial ………thank you

  16. Durga Bhavani says:

    Hi,
    please develop the jsf tutorial as early as possible.we are waiting for that.

  17. Sumanth says:

    Hi Sivateja Kandula,

    This tutotial is very nice. Now I want To Connect With 2 Databases For Example MySql, Oracle.Can You Provide how to configure this..??

  18. creditto says:

    Hai Bro…

    Very Useful Site And feelling like Very Easy to Learn

  19. Srinivas says:

    No need of any training… This content is enough to learn Hibernate

  20. amit_0 says:

    thanks for your tutorials they really helped me….

  21. sandip says:

    wow really good sir, it’s very simple for me thanks sir

  22. Edison OKPE says:

    Great work ! hibernate simplified Thanks

  23. Lalchand Mali says:

    Great Tutorial..
    I have read more tutorial but found this one is great.

    Thanks a lot

  24. jagadeesh says:

    Niced tutorial ,Gr8 work

  25. bala says:

    very simple and easy to understand syntax’s

  26. rajnishpatel says:

    thank you very much for writing hibernate tutorials ….its easy to understand again i want to say thanks a lot,its very very helpful ………….

  27. Thaqi says:

    Is there any PDF doc for this?

  28. Devendra says:

    This is very nice tutorial website for beginner as well as professional.

    Thanks Sivateja Kandula for your outstanding efforts

  29. suresh says:

    very good site to learn every topic

  30. Anbarasu says:

    Thank u so much..

  31. srinivas says:

    i have one doubt is it possible to write combination of hbm and annotations for single programe.

  32. Sudhakar says:

    Hai Sivateja,
    Your explanation very good…
    Thanks…

  33. Mallikarjun Patil says:

    Hi Sivateja,

    Very helpfull for beginners and experience people, keep posting help full java related technology.

    Thanks,

  34. Prasanna says:

    Very nice and understandable tutorial. Thank you sooooooo much…

  35. Nagendra says:

    very nice this hibernate tutorial its very simplest explanations..

  36. pradeep says:

    any external mapping is needed wheather we use annotations please explain in depth while using the annotations

  37. chirag baraiya says:

    sir this is very nice tutorial Please explain me Getters and Setters method with example i can’t understand

  38. priyanka says:

    Can we give mapping file name other than .hbm.xml extension.

  39. Abdul says:

    Excellent work….

    Hibernate simplified….

    Thanks bro

  40. Rahul Gupta says:

    nice tutorial

  41. shailesh says:

    Easy to understand language..very good work!

  42. suseela says:

    awesom site

  43. Sandeep Saini says:

    Sir why mostly XML is considered in place of .properties file.

  44. Animesh says:

    Can you please give an example of mapping file in terms of annotations ?
    syntax of Mapping Annotations !…

  45. ved prakash says:

    superb.

  46. Rajesh says:

    Simply Superb…

  47. k.s.Banu says:

    super nd simplly explanation

  48. Vijayalaxmi says:

    This tutorial is very simple to learn hibernate…
    Thank u Sir

  49. Good One!! Appreciated !!

  50. Amulya says:

    Superb and very explanatory!

  51. Ravi says:

    Hi Siva,

    This hibernate tutorial is Awesome.

    Thanks.

  52. riyas says:

    Awesome.

  53. Anchal says:

    Hi,

    Is configuration file can be multiple??

  54. Mani says:

    Wow simply superb

  55. Sai Ganesh says:

    This hibernate tutorial is Awesome. it’s very simple for me thank you…

  56. satya says:

    The way of ur explanation was simple and good.

  57. Konark says:

    I had this fear in me of will I be able to manage to learn on my own, bt after reading this it has built up a confidence in me that I can. Thanks to the author for explaining it in a very simplified language.

  58. P.Ramesh says:

    Thank you sir, my long waiting of learning hibernate is now fulfilled.

  59. Rajesh says:

    Please post the details of integration of spring with hibernate and sessionfactory object integration and firstlevel cache working functionality

Name*
Mail*
Website



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.