How to Configure hibernate using XML files?

Hibernate Configuration

Using XML files for configuring hibernate application is the most widely used approach. This is simple one and less whanges needed in the future. There is two types of configuration we can do using XML files. one is non-managed environment and another one is for managed environment. Here we will explain about the non-managed environment or standalone program. In the managed environment you can use datasource for establishing connection to the database instead directly specifying all the properties in the configuration file.

also read:

This sample program uses configuration.configure().buildSessionFactory() to configure the hibernate application. It assumes that the hibernate.cfg.xml file is placed in the classpath of the project. if you want to change the configuration file name and path then pass as argument in the configuration.configure(“filename”) method.


import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

 * source :
public class JavaBeatHibernateExample {
    public static void main(String args[]){
        Configuration configuration = new Configuration();

        // configuring hibernate
        SessionFactory  sessionFactory = configuration.configure().buildSessionFactory();


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        <property name="connection.url">jdbc:derby://localhost:1527/SampleDB
        <property name="connection.username">root
        <property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver
        <property name="dialect">org.hibernate.dialect.DerbyDialect
        <property name="connection.password">root
        <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory
        <property name="current_session_context_class">thread
        <property name="hibernate.show_sql">true

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!