Hibernate Configuration files
Hibernate is an ORM (Object - Relational mapping) framework. It means that all the entity classes created using hibernate API represent tables in database and the objects created out from each class represent a row of a table. Analogy of object and database can be represented as..
Java Class -> Database Table
Java instance variables -> Database Table columns
Java Object/instance -> Table Row
To register the relation between java classes and tables in hibernate, we need to configure the mapping between in a configuration file. We call it 'Employee.hbm.xml'.
There is one more configuration file which configures the connection between an application and database as well as some configurations exclusive to hibernate. It is hibernate.cfg.xml
Basic hibernate configuration file looks like below
<hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/dbname</property> <property name="connection.username">root</property> <property name="connection.password">admin</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property> <!-- Names the annotated entity class --> <mapping class="com.javarephrased.hibernatetutorial.entity.Employee"/> <!--<mapping resource="com/javarephrased/hibernatetutorial/entity/Employee.hbm.xml"/>--> </session-factory> </hibernate-configuration>
All the connection.* properties defined in the first 4 lines configures database used in the application.
Mapping class & Mapping resource
Mapping class is an entity class which maps instance variables and the table columns. Hibernate annotations are used for this mapping.Mapping resource defines an XML file to declare the mappings b/w java instance variables and table columns. .hbm.xml is the file convention.
show_sql(true/false)
Prints all the SQL statements that hibernate generates to console. Supported for debugging, query analysis.
cache.provider_class
Used for 2nd level caching. You can disable this by providing 'org.hibernate.cache.internal.NoCacheProvider'.
Caching increases performance of an application by avoiding the network trips from application to database for read operation. We can register a 3rd party cache provider using this tag.
Caching increases performance of an application by avoiding the network trips from application to database for read operation. We can register a 3rd party cache provider using this tag.
current_session_context_class(jta/thread/managed)
There is one more analogy between hibernate and database which is session. It is analogous to transaction in database.
This tag initializes the scope of session which is obtained from getCurrentSession() method of hibernate API.
This tag initializes the scope of session which is obtained from getCurrentSession() method of hibernate API.
Session session = sessionFactory.getCurrentSession()
Session is used as a unit of work in hibernate.
Session is used as a unit of work in hibernate.
hbm2ddl.auto(create/update/validate/create-drop):
By enabling this property, hibernate takes care of creating the tables in database.
Drops all the tables and creates all the tables every time you restart the application.
update:
Creates the tables only if the tables are not available in the database during restart of your application.
validate:
checks whether the tables are present or not. It does not create any tables. it is just used to validate whether the entity objects are compatible with tables of your database. It can also be useful if we don't want to disturb the schema/tables.
create-drop:
creates all the tables every time you restart the application just like create does. But, drops the tables once sessionFactory.close() is called.
create-drop:
creates all the tables every time you restart the application just like create does. But, drops the tables once sessionFactory.close() is called.
dialect:
Dialect basically acts as a translator which converts hibernate entity operations such as save(), update() to SQL queries. Hibernate provides dialect classes for every SQL databases like Oracle, MySQL, DB2, etc.,
The dialect is in place because, there can be scenarios where hibernate cannot create a generic query that can run in all the SQLs. By providing the dialect of the database we are using to hibernate, hibernate can make the necessary changes to run the query on that database.
All the dialect classes in hibernate are available in ‘org.hibernate.dialect.*’ package.
Comments
Post a Comment