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.
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.
J2eeBrainHibernateExample.java
package j2eebrain.com.hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* source : www.j2eebrain.com
*/
public class J2eeBrainHibernateExample {
public static void main(String args[]){
Configuration configuration = new Configuration();
// configuring hibernate
SessionFactory sessionFactory = configuration.configure().buildSessionFactory();
}
}
hibernate.cfg.xml
......
Source:
http://www.j2eebrain.com/java-J2ee-configuring-hibernate.html
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.
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.
J2eeBrainHibernateExample.java
package j2eebrain.com.hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* source : www.j2eebrain.com
*/
public class J2eeBrainHibernateExample {
public static void main(String args[]){
Configuration configuration = new Configuration();
// configuring hibernate
SessionFactory sessionFactory = configuration.configure().buildSessionFactory();
}
}
hibernate.cfg.xml
......
Source:
http://www.j2eebrain.com/java-J2ee-configuring-hibernate.html
Search News
News Categories
What's the News?
Post a link to something interesting from another site, or submit your own original writing for the Java community to read.
Most Popular News
-
How to stand out from other Java/JEE Professionals?
Published about 14-01-2009 | Rated +3 -
10 reasons IT certification will be important in 2009
Published about 05-01-2009 | Rated +2 -
The 9 hottest skills for `09
Published about 02-01-2009 | Rated +1 -
New Features in Servlets 3.0
Published about 05-01-2009 | Rated +4
Most Recent User Submitted News
- 45+ New jQuery Techniques For Good User Experience
Published about 16-01-2009 | Rated +1 - Using a database transaction with JDBC
Submitted by Balamurali | Rated 0 - Connect to database and call stored procedure
Submitted by Balamurali | Rated 0 - Oracle JDeveloper (10.1.3.5) (Build 4306)
Published about 23-08-2009 | Rated 0







