• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to Configure hibernate using XML files?

July 19, 2008 //  by Krishna Srinivasan//  Leave a Comment

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:

  • Introduction to Hibernate
  • Hibernate Interview Questions
  • Interceptors in Hibernate
  • Hibernate Books

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.

JavaBeatHibernateExample.java

package javabeat.net.hibernate;

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

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

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

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>

Category: HibernateTag: hibernate configuration

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Creating simple toolbar using RichFaces tag library
Next Post: Constructor Injection in Spring IOC »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact