Hibernate basic configuration example

July 13, 2008

Hibernate

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.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 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

1
2
3
4
5
6
7
8
9
10
<?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>
email

Comments

comments

  • Anindita09 Roy

    Hi i have added ur code.It is realy nice. But I am getting one error.
    INFO: Cache provider: org.hibernate.cache.EhCacheProvider
    Exception in thread “main” java.lang.NoClassDefFoundError: net/sf/ehcache/CacheException
        at java.lang.Class.getDeclaredConstructors0(Native Method)
        at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
        at java.lang.Class.getConstructor0(Unknown Source)
        at java.lang.Class.newInstance0(Unknown Source)
        at java.lang.Class.newInstance(Unknown Source)
        at org.hibernate.cfg.SettingsFactory.createCacheProvider(SettingsFactory.java:346)
        at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:220)
        at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1928)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1211)
        at javabeat.net.hibernate.JavaBeatHibernateExample.main(JavaBeatHibernateExample.java:29)
    Caused by: java.lang.ClassNotFoundException: net.sf.ehcache.CacheException
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        … 10 more

    Then I have added  ehcache-1.5.0.jar in the class path and got
    Exception in thread “main” java.lang.NoClassDefFoundError: edu/emory/mathcs/backport/java/util/concurrent/BlockingQueue
        at net.sf.ehcache.config.ConfigurationHelper.createCache(ConfigurationHelper.java:418)
        at net.sf.ehcache.config.ConfigurationHelper.createDefaultCache(ConfigurationHelper.java:334)
        at net.sf.ehcache.CacheManager.configure(CacheManager.java:306)
        at net.sf.ehcache.CacheManager.init(CacheManager.java:226)
        at net.sf.ehcache.CacheManager.(CacheManager.java:213)
        at org.hibernate.cache.EhCacheProvider.start(EhCacheProvider.java:124)
        at org.hibernate.impl.SessionFactoryImpl.(SessionFactoryImpl.java:180)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1213)
        at javabeat.net.hibernate.JavaBeatHibernateExample.main(JavaBeatHibernateExample.java:29)
    Caused by: java.lang.ClassNotFoundException: edu.emory.mathcs.backport.java.util.concurrent.BlockingQueue
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        … 9 more
     

  • Pingback: JavaPins

  • x

    00

  • Vishal G

    Incomplete.