JavaBeat

  • Home
  • 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)
  • Privacy

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

[code lang=”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();
}
}
[/code]

hibernate.cfg.xml

[code lang=”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>

[/code]

Filed Under: Hibernate Tagged With: hibernate configuration

Hibernate basic configuration example

July 13, 2008 by Krishna Srinivasan Leave a Comment

Hibernate Configuration

This example demonstrates how to configure hibernate framework for running a simple standalone program. Here the sample program uses programmatic configuration to set all the properties required for running hibernate. Also the example uses derby as the database to connect and update the values. This is not the big change, you only have to change few parameters if you are using any other databases.

also read:

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

The following are the jar files required to run this example:

  • hibernate3.jar
  • dom4j.jar
  • commons-logging.jar
  • derby.jar
  • commons-collections.jar
  • cglib.jar
  • asm.jar
  • cuncurrent.jar
  • jta.jar

JavaBeatHibernateExample.java

[code lang=”java”]
package javabeat.net.hibernate;

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

/**
*
* source : www.javabeat.net
*/
public class JavaBeatHibernateExample {
public static void main(String args[]){
Configuration configuration = new Configuration();
configuration.addClass(javabeat.net.hibernate.EmployeeInfo.class);
configuration.setProperty("hibernate.dialect",
"org.hibernate.dialect.DerbyDialect");
configuration.setProperty("hibernate.connection.url",
"jdbc:derby://localhost:1527/SampleDB");
configuration.setProperty("hibernate.connection.username", "root");
configuration.setProperty("hibernate.connection.driver_class",
"org.apache.derby.jdbc.ClientDriver");
configuration.setProperty("hibernate.connection.password", "root");
configuration.setProperty("hibernate.transaction.factory_class",
"org.hibernate.transaction.JDBCTransactionFactory");
configuration.setProperty("hibernate.current_session_context_class",
"thread");
configuration.setProperty("hibernate.show_sql", "true");
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
transaction.begin();
EmployeeInfo employeeInfo = new EmployeeInfo();
employeeInfo.setSno(1);;
employeeInfo.setName("KamalHasan");
session.save(employeeInfo);
transaction.commit();
session.close();
}
}
[/code]

EmployeeInfo.java

[code lang=”java”]
package javabeat.net.hibernate;

/**
* source : www.javabeat.net
*/
public class EmployeeInfo {
private int sno;
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getSno() {
return sno;
}

public void setSno(int sno) {
this.sno = sno;
}
}
[/code]

EmployeeInfo.hbm.xml

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="javabeat.net.hibernate.EmployeeInfo" table="Employee_Info">
<id name="sno" column="sno" type="java.lang.Integer">
</id>
<property name="name" column="name" type="java.lang.String"/>
</class>
</hibernate-mapping>[/code]

Filed Under: Hibernate Tagged With: hibernate configuration

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved