Powered by Blogger.

Hibernate communication with RDBMS

>> Saturday, March 19, 2011

General steps:

1. Load the Hibernate configuration file and create configuration object. It will automatically load all hbm           mapping files.
2. Create session factory from configuration object
3. Get one session from this session factory.
4. Create HQL query.
5. Execute query to get list containing Java objects.

Example: Retrieve list of employees from Employee table using Hibernate. /* Load the hibernate configuration file */

Configuration cfg = new Configuration(); cfg.configure(CONFIG_FILE_LOCATION);

/* Create the session factory */

SessionFactory sessionFactory = cfg.buildSessionFactory();

/* Retrieve the session */

Session session = sessionFactory.openSession();

/* create query */

Query query = session.createQuery("from  EmployeeBean”);

/* execute query and get result in form of Java objects */ List finalList = query.list();

EmployeeBean.hbm.xml File

"-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2014. All rights reserved.