Creating Session Object Using hibernate.cfg.xml file Hibernate 3.0, MySql
In this tutorial example I will demonstrate how to create org.hibernate.SessionFactory object in Hibernate 3.0 and get org.hibernate.Session object from it.For doing this example we need hibernate configuration file (hibernate.cfg.xml) and one java class where we can write code to create org.hibernate.SessionFactory object.
Before starting I am assuming that you can set up hibernate 3.0 development environment in eclipse IDE.
So Please follow the steps to complete this example.
Create a hibernate.cfg.xml file
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
| <?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"<hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.username">root</property> <property name="connection.password">mysql</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> <property name="current_session_context_class">thread</property> </session-factory></hibernate-configuration> |
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
| package com.hibernate.dBConnection;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtils { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { return new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } public static void main(String[] args) { SessionFactory factory = getSessionFactory(); System.out.println("Session factory object created : " + factory); Session session = factory.openSession(); try { System.out.println("Session object created : " + session); // We can use this session object for doing CRUD (inserting, // updating and deleting rows) } catch (Exception e) { e.printStackTrace(); } finally { session.close(); } }} |
** In case file hierarchy is not correct you may get error in loading hibernate.cfg.xml. means you will not get SessionObject created**
So Please below eclipse image which is showing the hierarchy of this example.
We can see the SessionFactory object in the output like this :
Please let me know if you have any error while compiling or while running this example.
0 comments
Post a Comment