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. hbm2ddl.auto: 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
org.hibernate.dialect.DB2Dialect
2. DB2 AS/400
org.hibernate.dialect.DB2400Dialect
3. DB2 OS390
org.hibernate.dialect.DB2390Dialect
4. PostgreSQL
org.hibernate.dialect.PostgreSQLDialect
5. MySQL
org.hibernate.dialect.MySQLDialect
6. MySQL with InnoDB
org.hibernate.dialect.MySQLInnoDBDialect
7. MySQL with MyISAM
org.hibernate.dialect.MySQLMyISAMDialect
8. Oracle 8
org.hibernate.dialect.OracleDialect
9. Oracle 9i/10g
org.hibernate.dialect.Oracle9Dialect
10. Sybase
org.hibernate.dialect.SybaseDialect
11. Sybase Anywhere
org.hibernate.dialect.SybaseAnywhereDialect
12. Microsoft SQL Server
org.hibernate.dialect.SQLServerDialect
13. SAP DB
org.hibernate.dialect.SAPDBDialect
14. Informix
org.hibernate.dialect.InformixDialect
15. HypersonicSQL
org.hibernate.dialect.HSQLDialect
16. Ingres
org.hibernate.dialect.IngresDialect
17. Progress
org.hibernate.dialect.ProgressDialect
18. Mckoi SQL
org.hibernate.dialect.MckoiDialect
19. Interbase
org.hibernate.dialect.InterbaseDialect
20. Pointbase
org.hibernate.dialect.PointbaseDialect
21. FrontBase
org.hibernate.dialect.FrontbaseDialect
22. Firebird
org.hibernate.dialect.FirebirdDialect
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
@Id
@Column
@Entity
@Table
@GeneratedValue
@Temporal
* 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