How to configure hibernate?
Hello everybody, I am java programmer and i have came across hibernate now. Though i am beginner in this programming world. I want to use hibernate in my java project. I know little bit about this hibernate but still it would be helpful for me if somebody can tell me that how to configure hibernate.
Hibernate is configured in two steps.
Hibernate is the service which helps you to make difference between the front end and back end of your application. Basically hibernate is configured in two steps. First step is to configure the hibernate service. Configuring the hibernate service includes database connection parameters, collection of persistent classes and caching. And second step is to provide information about the classes to be persisted. These classes would be helping you to bridge gaps between the class and databases.
Configure hibernate by hibernate.properties or hibernate.cfg.xml
If you want to configure the hibernate on your computer then you may do it by by using one of the configurations file provided by the hibernate. There are two configurations file in order to configure the hibernate, One is hibernate.properties which is a standard java properties file and second is hibernate.cfg.xml. Both of these file performs the same functions. If you application classpath contains these both files then hibernate.cfg.xml overrides the settings found in the hibernate.properties file.
What hibernate.cfg.xml contains.
Here is the code written in hibernate.cfg.xml file to configure the hibernate in a particular application.
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:derby://localhost:1527/SampleDB
</property>
<property name="connection.username">root
</property>
<property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver
</property>
<property name="dialect">org.hibernate.dialect.DerbyDialect
</property>
<property name="connection.password">root
</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory
</property>
<property name="current_session_context_class">thread
</property>
<property name="hibernate.show_sql">true
</property>
</session-factory>
</hibernate-configuration>
Hibernate provided JDBC connections.
If you are making an application with the hibernate provided JDBC connections then your hibernate configuration file must have the following properties.
1. connection.driver_class which will be containing the JDBC connection class for the specific database.
2. connection.url which represents full JDBC URL to the database.
3. connection.username which will be used to connect to the database.
4. connection.password to authenticate the username.
5. dialect specifying the name of the SQL dialect for the database.
Configuring hibernate by using XML file.
You can configure the hibernate in your application by using XML files. This is the simplest and widely used approach. There are total two types of configuration that you can do using XML files. one is non-managed environment and another one is for managed environment. In managed environment, You can use source of database to establishing connection rather then specifying all the properties in the configuration file.