To inform Hibernate from where to find mapping information of Java classes to database tables or configuration setting related to database, All such information is usually supplied by Hibernate Configuration Object. It is managed by an instance of org.hibernate.cfg.Configuration. An instance of org.hibernate.cfg.Configuration represents an entire set of mappings of an application’s Java types to an SQL database. The mappings are compiled from various XML mapping files or from Java 5 Annotations.
Hibernate provides following types of configurations
2. hibernate.properties file :
This is the easiest way to get started with Hibernate. Create a file hibernate.properties and place it in root of your applications CLASSPATH.
Below is the sample hibernate.properties file:
We can obtain a org.hibernate.cfg.Configuration instance by instantiating it directly and specifying XML mapping documents. If the mapping files are in the classpath, use addResource(). For example:
An alternative way is to specify the mapped class and allow Hibernate to find the mapping document for you:
A org.hibernate.cfg.Configuration also allows you to specify configuration properties. For example:
Refer here for more details :
http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/session-configuration.html#configuration-optional
Hibernate provides following types of configurations
- hibernate.cfg.xml – A standard XML file which contains hibernate configuration and which resides in root of application’s CLASSPATH
- hibernate.properties – A Java compliant property file which holds key value pair for different hibernate configuration strings.
- Programmatic configuration – This is the manual approach. The configuration can be defined in Java class.
1. hibernate.cfg.xml : The hibernate.cfg.xml
file is a standard XML file which contains all the configuration
parameters like database connection, class mappings etc. This file
needs to be placed root of CLASSPATH of application.
Below is the sample hibernate.cfg.xml file :
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration> <session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/SampleApp</property>
<property name="connection.username">root</property>
<property name="connection.password">root123</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.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<!-- Mapping files POJO -->
<mapping class="com.hibernate.pojo.Host"/>
</session-factory>
</hibernate-configuration>
2. hibernate.properties file :
This is the easiest way to get started with Hibernate. Create a file hibernate.properties and place it in root of your applications CLASSPATH.
Below is the sample hibernate.properties file:
hibernate.connection.driver_class=com.mysql.jdbc.Driver3. Programmatic Configuration
hibernate.connection.url=jdbc:mysql://localhost/SampleApp
hibernate.connection.username=root
hibernate.connection.password=root123
hibernate.connection.pool_size=1
hibernate.dialect = org.hibernate.dialect.MySQLDialect
We can obtain a org.hibernate.cfg.Configuration instance by instantiating it directly and specifying XML mapping documents. If the mapping files are in the classpath, use addResource(). For example:
Configuration cfg = new Configuration()
.addResource("Host.hbm.xml");
An alternative way is to specify the mapped class and allow Hibernate to find the mapping document for you:
Configuration cfg = new Configuration()
.addClass(com.hibernate.pojo.Host.class);
A org.hibernate.cfg.Configuration also allows you to specify configuration properties. For example:
Configuration cfg = new Configuration()
.addClass(com.hibernate.pojo.Host.class)
.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect")
.setProperty("hibernate.connection.datasource", "jdbc:mysql://localhost/SampleApp")
.setProperty("hibernate.order_updates", "true");
Refer here for more details :
http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/session-configuration.html#configuration-optional