Java Application makes use of hibernate API methods calls to inform the persistent needs to hibernate. Then Hibernate engine generate JDBC code that corresponds to the underlying DB by using mapping file and configuration file information. We can also find the architecture diagrams as follows
Hibernate mapping file
- In this file hibernate application developer specify the mapping from entity class name to table name and entity properties names to table column names. i.e. mapping object oriented data to relational data is done in this file
- Standard name for this file is <domain-object-name.hbm.xml>
- In general, for each domain object we create one mapping file
Number of Entity classes = that many number of mapping xmls
- Mapping can be done using annotations also. If we use annotations for mapping then we no need to write mapping file.
- From hibernate 3.x version on wards it provides support for annotation, So mapping can be done in two ways
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>
Hibernate configuration file
- It is an XML file in which database connection details (username, password, url, driver class name) and, Hibernate Properties(dialect, show-sql, second-level-cache … etc) and Mapping file name(s) are specified to the hibernate
- Hibernate uses this file to establish connection to the particular database server
- Standard name for this file is <hibernate.cfg.xml>
- 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 file
- Properties file(o1d style)
- We don’t have annotations to write configuration details. Actually in hibernate l.x, 2.x we defined this configuration by using .properties file, but from 3.x XML came into picture. XML files are always recommended to use.
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>