Monday, 11 March 2019

Introduction - Hibernate Configuration File

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC  
          "-//Hibernate/Hibernate Configuration DTD 5.3//EN"  
          "http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd">

<hibernate-configuration>
<session-factory>  
        <property name="hbm2ddl.auto">update</property>  
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>  
        <property name="connection.url">jdbc:mysql://localhost:3306/bs</property>  
        <property name="connection.username">root</property>  
        <property name="connection.password">7901</property>  
        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>  
    <property name="show_sql">true</property>
    <mapping resource="Student.hbm.xml"/> 
    </session-factory>  
</hibernate-configuration> 

=======================================================================
More About Configuration File:

Line #1. 
<?xml version="1.0" encoding="UTF-8"?> : This line shows the document type and version.
Most of xml files start with this line.

Line #2.
<!DOCTYPE hibernate-configuration PUBLIC  
          "-//Hibernate/Hibernate Configuration DTD 5.3//EN"  
          "http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd">:
The DTD in the hibernate jar is a good way to know what are the attributes that can be included and the expected name for that tag.

Note: DTD is vary HB version to version.

Line #6.
<hibernate-configuration>: Each configuration file start by this tag.
Note: ONLY one configuration file is enough for per project.
We can configure more than one DB connection by single configuration file.

Line #7.
<session-factory> : For each DB configure we need ONE session-factory.
If we configure HB application by two DB then we need two <session-factory> tags.

Line #8.
<property name="hbm2ddl.auto">update</property>: This lines performs auto DDL opeartion.
We will discuss later about this line.
This is optional if we don't use then we before run the program we have to create DB Tables manually.

Line #9.
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>: 
hibernate.dialect property makes Hibernate to generate the appropriate SQL statements for the chosen database. Dialect is the SQL dialect that your database uses. Hibernate uses "dialect" configuration to know which database you are using so that it can convert hibernate query to database specific query.

For MySQL-5 or Lower version:
org.hibernate.dialect.MySQL5Dialect

Above For MySQL-5:
org.hibernate.dialect.MySQL8Dialect

For Oracle Database:
org.hibernate.dialect.OracleDialect

Line #10.
<property name="connection.url">jdbc:mysql://localhost:3306/bs</property>  : This lines defines the URL for the Database:

For mysql:
jdbc:mysql://localhost:3306/bs      Note: bs is database name, so you can change accordingly

For Oracle:
jdbc:oracle:thin:@localhost:1521:xe   Note: it is for TYPE-IV driver


Line #11.
<property name="connection.username">root</property> : User name of your database.

Line #12.
<property name="connection.password">7901</property> : Password of the database.

Line #13.
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>  : Path of your driver class.
Note: Please check the driver PATH in your Database jar files:
MOSTLY:
For mysql5 or lower version:
com.mysql.jdbc.Driver

For Oracle:
oracle.jdbc.driver.OracleDriver

Line #14.
<property name="show_sql">true</property> : It is optional. If we used this line then it show the sql commands on the console that Hibernate internally used.

Line #15.
<mapping resource="Student.hbm.xml"/> : It is used to add the mapping file.
We can add many mapping files for example:
<mapping resource="Student.hbm.xml"/>
<mapping resource="Employee.hbm.xml"/>
<mapping resource="Phone.hbm.xml"/>



No comments:

Post a Comment

Access attributes in component

NOTE: To access an attribute in a  component , use expressions as  {! v.<Attribute Name>} . ----------------------------------------...