Newsletter |
Mapping And Configuration Files In Hibernate
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 .:: | ||
very nice tutorials…….
Thanks a lot…
Really a simple and nice tutorial to learn in easily Hibernate. Thanks a lot. I am going to bookmark it.
Excellent way of explaining..quite easy and understandable… hope I am going to learn hibernate this time 🙂
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?
Nice One I appreciate this work
simple and easily understandable to every one …
thank u for giving such a great tutorial….
Very Useful Site And feelling like Very Easy to Learn
Thanks Bro. It made my life easy.
Its Really a Good job.
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.
Which version of hibernate we should download to run through the trail examples mentioned in the website
Very Nice….. I appreciate this work…
thank you very much..
Superb….No words !!
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
Hibernate is the responsible for opening and closing the connection. we no need to open and close connections.
tutorials are very useful…
nice tutorial ………thank you
Hi,
please develop the jsf tutorial as early as possible.we are waiting for that.
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..??
Hai Bro…
Very Useful Site And feelling like Very Easy to Learn
No need of any training… This content is enough to learn Hibernate
thanks for your tutorials they really helped me….
wow really good sir, it’s very simple for me thanks sir
Great work ! hibernate simplified Thanks
Great Tutorial..
I have read more tutorial but found this one is great.
Thanks a lot
Niced tutorial ,Gr8 work
very simple and easy to understand syntax’s
thank you very much for writing hibernate tutorials ….its easy to understand again i want to say thanks a lot,its very very helpful ………….
Is there any PDF doc for this?
This is very nice tutorial website for beginner as well as professional.
Thanks Sivateja Kandula for your outstanding efforts
very good site to learn every topic
Thank u so much..
i have one doubt is it possible to write combination of hbm and annotations for single programe.
Hai Sivateja,
Your explanation very good…
Thanks…
Hi Sivateja,
Very helpfull for beginners and experience people, keep posting help full java related technology.
Thanks,
Very nice and understandable tutorial. Thank you sooooooo much…
very nice this hibernate tutorial its very simplest explanations..
any external mapping is needed wheather we use annotations please explain in depth while using the annotations
sir this is very nice tutorial Please explain me Getters and Setters method with example i can’t understand
Can we give mapping file name other than .hbm.xml extension.
Excellent work….
Hibernate simplified….
Thanks bro
nice tutorial
Easy to understand language..very good work!
awesom site
Sir why mostly XML is considered in place of .properties file.
Can you please give an example of mapping file in terms of annotations ?
syntax of Mapping Annotations !…
superb.
Simply Superb…
super nd simplly explanation
This tutorial is very simple to learn hibernate…
Thank u Sir
Good One!! Appreciated !!
Superb and very explanatory!
Hi Siva,
This hibernate tutorial is Awesome.
Thanks.
Awesome.
Hi,
Is configuration file can be multiple??
Wow simply superb
This hibernate tutorial is Awesome. it’s very simple for me thank you…
The way of ur explanation was simple and good.
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.
Thank you sir, my long waiting of learning hibernate is now fulfilled.
Please post the details of integration of spring with hibernate and sessionfactory object integration and firstlevel cache working functionality