||Struts 1.x 2.x | Spring | Strips | Wicket | Tapestry | Seam | JSF | RIFE | DWR | DOJO | EXT | Jquery | Json | Prototype | Hibernate | iBatis | web 2.0 | www.siva2baba.com & shivababa@gmail.com
   
  FrameWorks Theater
  Hibernate
 

Hibernate


Hibernate
is a powerful, high performance object/relational persistence and query service. Hibernate lets you develop persistent classes following object-oriented idiom - including association, inheritance, polymorphism, composition, and collections. Hibernate allows you to express queries in its own portable SQL extension (HQL), as well as in native SQL, or with an object-oriented Criteria and Example API.


Hibernate language, providing a is an object-relational mapping (ORM) library for the Javaframework for mapping an object-orienteddomain model to a traditional relational database. Hibernate solves Object-Relational impedance mismatch problems by replacing direct persistence-related database accesses with high-level object handling functions.


Hibernate not only takes care of the mapping from Java classes to database tables (and from Java data types to SQL data types), but also provides data query and retrieval facilities and can significantly reduce development time otherwise spent with manual data handling in SQL and JDBC. .

Features of Hibernate

  • Hibernate 3.0 provides three full-featured query facilities: Hibernate Query Language, the newly enhanced Hibernate Criteria Query API, and enhanced support for queries expressed in the native SQL dialect of the database.
      
  • Filters for working with temporal (historical), regional or permissioned data.
       
  • Enhanced Criteria query API: with full support for projection/aggregation and subselects.
      
  • Runtime performance monitoring: via JMX or local Java API, including a second-level cache browser.
      
  • Eclipse support, including a suite of Eclipse plug-ins for working with Hibernate 3.0, including mapping editor, interactive query prototyping, schema reverse engineering tool.
       
  • Hibernate is Free under LGPL: Hibernate can be used to develop/package and distribute the applications for free.
       
  • Hibernate is Scalable: Hibernate is very performant and due to its dual-layer architecture can be used in the clustered environments.
      
  • Less Development Time: Hibernate reduces the development timings as it supports inheritance, polymorphism, composition and the Java Collection framework.
      
  • Automatic Key Generation: Hibernate supports the automatic generation of primary key for your.
      
  •  JDK 1.5 Enhancements: The new JDK has been released as a preview earlier this year and we expect a slow migration to the new 1.5 platform throughout 2004. While Hibernate3 still runs perfectly with JDK 1.2, Hibernate3 will make use of some new JDK features. JSR 175 annotations, for example, are a perfect fit for Hibernate metadata and we will embrace them aggressively. We will also support Java generics, which basically boils down to allowing type safe collections.
     
  • EJB3-style persistence operations: EJB3 defines the create() and merge() operations, which are slightly different to Hibernate's saveOrUpdate() and saveOrUpdateCopy() operations. Hibernate3 will support all four operations as methods of the Session interface.
      
  • Hibernate XML binding enables data to be represented as XML and POJOs interchangeably.
      
  • The EJB3 draft specification support for POJO persistence and annotations.
Two type of configuration on Hibernate
    ->
programmatic configuration

    ->xml Configuration(hibernate.cfg.xml)

programmatic configuration

Configuration config = new Configuration()
.addClass(org.hibernate.quickstart.Applabsuser.class)
.setProperty("hibernate.dialect", "org.hibernate.dialect. MySQLMyISAMDialect")
.setProperty("hibernate.connection.driver_class", " org.gjt.mm.mysql.Driver")



xml Configuration

hibernate.cfg.xml.- On startup,Hibernate consults this XML file for its operating properties,
such as-> database connection string ->password,
-->database dialect,and mapping files locations. Hibernate searches for this file on the classpath.

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

 

<session-factory>

   

<property name="show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLMyISAMDialect</property>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/applabs</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">r00Tp@$wd</property>

<mapping resource="org/applabs/hibernate/quickstart/Applabsuser.hbm.xml"/>

  </session-factory>
</hibernate-configuration> 



*(hibernate).hbm.xml-The second type of configuration file is a mapping description file (file extension *.hbm) that instructs Hibernate how to map data between a specific Java class and one or more database tables. Normally in a single application,you would have one hibernate.cfg.xml file,and multiple .hbm files depending upon the business entities you would like to persist.

Here are some commonly used classes of Hibernate.


1.5.1 SessionFactory (org.hibernate.SessionFactory)


SessionFactory is a thread safe (immutable) cache of compiled mappings for a single database. Usually an application has a single SessionFactory. Threads servicing client requests obtain Sessions from the factory. The behavior of a SessionFactory is controlled by properties supplied at configuration time.


1.5.2 Session (org.hibernate.Session)


A session is a connected object representing a conversation between the application and the database. It wraps a JDBC connection and can be used to maintain the transactions. It also holds a cache of persistent objects,used when navigating the objects or looking up objects by identifier.


1.5.3 Transaction (org.hibernate.Transaction)


Transactions are used to denote an atomic unit of work that can be committed (saved) or rolled back together. A transaction can be started by calling session.beginTransaction() which actually uses the transaction mechanism of underlying JDBC connection,JTA or CORBA.A Session might span several Transactions in some cases.



SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

session.saveOrUpdate(user);
tx.commit();
session.close();



Step by step Development


       Hibernate
                ||---+siva.xml
                            -->hibernate.cfg.xml(database connection,mapping file location)
                            -->contact.hbm.xml(mapping Rl-Object to Pojo)

                ||-->Contact.java(pojo class)
                ||-->First.java



import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class FirstExample {
    public static void main(String[] args) {
        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();
                //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(6);
                contact.setFirstName("Deepak");
                contact.setLastName("Kumar");
                contact.setEmail("deepak_38@yahoo.com");
                //session.save(contact);
                System.out.println("Done");
        }catch(Exception e){
            System.out.println(e.getMessage());
        }finally{
            // Actual contact insertion will happen at this step
            //session.flush();
            //session.close();

            }
       
    }
}


           


Example program --http://siva2baba.diinoweb.com
 
  Today, there have been 1 visitors (26 hits) on this page! www.siva2baba.com & shivababa@gmail.com  
 
=> Do you also want a homepage for free? Then click here! <=
||Struts 1.x 2.x | Spring | Strips | Wicket | Tapestry | Seam | JSF | RIFE | DWR | DOJO | EXT | Jquery | Json | Prototype | Hibernate | iBatis | web 2.0 | http://siva2baba.diinoweb.com/files/ and www.siva2baba.com