Recent Posts

Sunday, 15 May 2016

More About Hibernate Configuration and Mapping Filles

More about hibernate.cfg.xml
* This xml file used to specify locations of mapping files/Entities
* In projects we don't give the database details(ur1, username, password, driverclass) in the configuration file, instead of that, we give JNDI name of DatatSource.
<property name="connection.datasource">dataSourceName</property>
* Hibernate slupports default connection pooling but which will not be used in projects We use always server provided connection pooling.

1. connection.pool_size: Used to configure hibernate provided connection pooling in hibernate.cfg.xml
<property name="connection.pool_size">10</property>
     A connection pool is a cache of database connections maintained so that the connections can be reused when future requests to the database are required. Connection pools are used to enhance the performance of executing commands on a database.

2. show-sql: if the 'show_sql' value is true we can view all the hibernate generated queries in the console.
<property name="show_sql">true</property>
3. use_sql_comments: To add SQL comment to the SQL query generated by Hibernate
<property name=" use_sql_comments">true</property>
4. format_sql: Format the SQL query, so that easy to read
<property name=format_sql>true</property>
5. It has two values
a. create or create-drop
b. update

a. Create
     If its value is create while running the application
Case 1: Table does not exist Create new schema based on the mapping file configurations
Case 2: Table exists Drop the existing schema and create a new schema based on the mapping file configurations

b. Update
     If its value is update while running the application
Case 1: table doesn't exist Create a new schema based on the mapping file configurations
Case 2: table exists Use the existing schema

About Dialect in Hibernate 
* Dialect class is a simple java class, which contains mapping between java language data type and database data type.
* Dialect class contains queries format for predefined hibernate methods.
* Hibernate generates queries for the specific database based on the Dialect class. If you want to shift from one database to another just change the Dialect class name in hibernate.cfg.xml file.
* All Dialect classes must extend 'Dialect' (abstract) class
* Hibernate supports almost 30 dialect classes.
* If we want we can write our own dialect by extending Dialect class
* Dialect class is used convert HQL queries into database specific queries.
1. DB2
2. DB2 AS/400
3. DB2 OS390
4. PostgreSQL
5. MySQL
6. MySQL with InnoDB
7. MySQL with MyISAM
8. Oracle 8
9. Oracle 9i/10g
10. Sybase
11. Sybase Anywhere
12. Microsoft SQL Server
13. SAP DB
14. Informix
15. HypersonicSQL
16. Ingres
17. Progress
18. Mckoi SQL
19. Interbase
20. Pointbase
21. FrontBase
22. Firebird
More about Hibernate mapping file
* Each hibernate mapping file must contain only one <id> (or relevant tag <composite-id>)
* Java object identified uniquely by the <id> tag property.
* <id> tag property corresponding column can be primary key or non-primary key in the database
* In mapping file class names and property names are names are not case sensitive.
* When the property name and column name both are same we no need to give 'column' attribute
* When the Persistent class name and table name both are same we no need to give 'table' attribute
* Generally we write one mapping file per one domain object. But it allows writing multiple objects mapping information within the same mapping file. Per each class mapping we need to write one <class> tag.
* Databases have different ways to organize its tables. Some database places all tables in a different "schemas", some database places all tables in a different "catalogs". If we want we can specify this in <class> tag of the mapping file
* In mapping file we write fully qualified name of the entity class in "name" attribute of <class> tag. Instead of that we can write package name separately using "package" attribute of <hibernate-mapping> tag
* In the mapping file we no need to map all the properties of the entity and all the columns of table. As per our requirement we configure required properties of the entity and columns of the table
     In our examples if we observe, we used the following annotations
* All the above annotations we are taking from java.persistence package. Actually this package is not the part of hibernate API. This package is from JPA(Java Persistence API).
* These annotations also given by hibernate. But we don't prefer to use hibernate given annotations. We prefer to use JPA annotations. Reason for this is, Hibernate is a specific API, where as JPA is a specification.
* If we use JPA annotations we have to flexibility to change the implementation vendor without changing application code.
* JPA is an APl(from SUN), its not the implementation. There are multiple implementations are there for JPA. Some of famous implementation of JPA are OpenJPA, Hibernate, Toplink Essentials, Eclipselink... etc.

Next Tutorial  Hibernate Configuration and Sessions

Previous Tutorial Setting Hibernate Environment

No comments:

Post a Comment