Magazine
 

Hibernate

 
  • PostgreSQL - org.hibernate.dialect.PostgreSQLDialect
  • Mckoi SQL - org.hibernate.dialect.MckoiDialect
  • Microsoft SQL Server - org.hibernate.dialect.SQLServerDialect
  • MySQL - org.hibernate.dialect.MySQLDialect
  • Oracle (any version) - org.hibernate.dialect.OracleDialect
  • Oracle 9 - org.hibernate.dialect.Oracle9Dialect
  • Progress - org.hibernate.dialect.ProgressDialect
  • FrontBase - org.hibernate.dialect.FrontbaseDialect
  • SAP DB - org.hibernate.dialect.SAPDBDialect
  • Sybase - org.hibernate.dialect.SybaseDialect
  • Sybase Anywhere - org.hibernate.dialect.SybaseAnywhereDialect

The <Session-Factory> property provides the
mechanism for managing persistent classes.
The <mapping
resource=”contact.hbm.xml”/>
property
refers to the mapping document that contains
mapping for domain object and hibernate
mapping document.

V. Hibernate Sample Code to Test (Inserting new record)

Now we are ready to write a program to insert the data into database. Firstly we understand about the Hibernate’s Session that is the main runtime interface between a Java application and Hibernate. The hibernate.SessionFactory allows application to create the Hibernate Sesssion reading the configuration from
hibernate.cfg.xml file. Then the save method as session.save(contact) on session object is used to save the contact information to the database:

Typical Hibernate program begins with configuration. It can be configured in two ways. Programmatically and Configuration file based.

 

In Configuration file based mode, hibernate looks for configuration file “hibernate.cfg.xml” in the claspath. It
creates mapping of tables and domain objects based on the provided resource mapping. While in the Programmatic configuration method, the JDBC connection details and
resource mapping details etc are supplied in the program using Configuration API.

Following example HibernateExample.java shows Configuration file based method of hibernate.

package roseindia.tutorial.hibernate;

import javax.transaction.Transaction;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
Session session = null;
try{
// This step will read hibernate.cfg.xml
and prepare hibernate for use
SessionFactory sessionFactory = new
Configuration().configure().buildSessionFactory();
session
=sessionFactory.openSession();
org.hibernate.Transaction tr =
session.beginTransaction();
//Create new instance of Contact
and set values in it by reading them from
form object
System.out.println(“Inserting
Record”);
Contact contact = new Contact();
contact.setId(3);
contact.setFirstName(“Nisha”);
contact.setLastName(“Gupta”);
contact.setEmail(“nishu_gpt@yahoo.com”);
session.save(contact);
tr.commit();
System.out.println(“Done”);

Dec  2007 | Java Jazz Up |31
 
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 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, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 ,

Download PDF