Вы находитесь на странице: 1из 8

Hibernate is a Object-relational mapping (ORM) tool for Java. used to map classes to tables, .

(row,column)

It was initiated by Gavin King in 2001.


Hibernate 4 was released in December 2011 Hibernate is a persistence framework.

features are:(SessionFactory,pojo classes,session,session object,config file,mapping file,persistent class) Advantages of hibernate 1) using annotation or XML file. 2) Java objects can be store directly and fetch from the database . 3) there is a change in any table or Database, you need to change only the XML file and the Java Class related to the table.. 4) need to work on familiar Java data types not sql. 5) It doesn't need any application server 6) simple query for data access.

What Should to know before starting hiber.


SupportedDatabase:(MySQL,oracle,postgresql,frontba

se.Microsoft SQL Server Database)


Hiber.Archi

Arch. Content Glance


hibernate.cfg.xml:symbolizes the complete mapping

between Java data types to the SQL database hibernate.properties:<property name="show_sql">true


</property>

SessionFactory : SessionFactory is responsible for creating a Session instances


to communicate with the database. SessionFactory object is a thread safe (immutable) and it

(sess.fact is resource intensive obj.) Transaction Management : This service allows for executing a several
is created once as per database

database statements at a time

hiber. Pojo class


package ajay; import javax.persistence.*; (sun stnd java)//not org.hiber.annotation @Entity public class Employee { @id private long empId; private String empName; //purpose is telling hibernate I want to create table out of this class public Employee() { } public Employee(String empName) { this.empName = empName; } public long getEmpId() { return this.empId; } public void setEmpId(long empId) { this.empId = empId; } public String getEmpName() { return this.empName; } public void setEmpName(String empName) { this.empName = empName; } }

Hiber. Config class


<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://192.168.10.13:3306/data </property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.connection.pool_size">10</property> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<mapping class="/ajay/Employee" /> //mapping pojo or persistence class


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

Testemp class

package com.hibernate.chapter1; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; //create table by calling hiber import org.hibernate.tool.hbm2ddl.SchemaExport; public class TestEmployee { public static void main(String[] args) { AnnotationConfiguration config=new AnnotationConfiguration(); config.addAnnotatedClass(Employee.class); config.configure("hibernate.cfg.xml"); new SchemaExport(config).create(true, true); //end create table; SessionFactory factory=config.buildSessionFactory(); Session session=factory.getCurrentSession(); session.beginTransaction(); Employee alex=new Employee(); alex.setEmpId(802); alex.setEmpName("ajay kumar"); session.save("alex"); session.getTransaction().commit();

Вам также может понравиться