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

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

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();
}
}

EmployeeInfo.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;
 }
}

EmployeeInfo.hbm.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>

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: « Accessing Managed Bean methods programmatically in JSF 1.1
Next Post: Batch insert in Hibernate »

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