Hibernate

aquahellishΛογισμικό & κατασκευή λογ/κού

13 Δεκ 2013 (πριν από 3 χρόνια και 9 μήνες)

92 εμφανίσεις

Hibernate

Bayu Priyambadha, S.Kom

What is Hibernate?


It is an object
-
relational mapping (ORM)
solution for Java


We make our data persistent by storing it
in a database


Hibernate takes care of this for us

Object
-
Relational Mapping


It is a programming technique for
converting object
-
type data of an object
oriented programming language into
database tables.


Hibernate is used convert object data in
JAVA to relational database tables.

Object/Relational Mapping

Presentation

Layer

Business

Layer

Persistence

Layer

Database

Object

Model

Relational

Model

Why Hibernate and not
JDBC?


JDBC maps Java classes to database
tables (and from Java data types to SQL
data types)


Hibernate automatically generates the
SQL queries.


Hibernate provides data query and
retrieval facilities and can significantly
reduce development time otherwise
spent with manual data handling in SQL
and JDBC.


Makes an application portable to all SQL
databases.

Hibernate vs. JDBC (an
example)


JDBC tuple insertion




st.executeUpdate(“INSERT INTO
book VALUES(“Harry
Potter”,”J.K.Rowling”));



Hibernate tuple insertion





session.save(book1);

Architecture

Hibernate sits between
your code and the
database

Maps persistent objects
to tables in the database

Hibernate Configuration

<?xml version="1.0" encoding="UTF
-
8"?>

<!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="hibernate.dialect">



org.hibernate.dialect.MySQLDialect


</property>



<property name="hibernate.connection.driver_class">



com.mysql.jdbc.Driver


</property>



<property name="hibernate.connection.url">



jdbc:mysql://localhost:3306/buku


</property>



<property name="hibernate.connection.username">



root


</property>


<mapping resource="model/Buku.hbm.xml"/>


</session
-
factory>

</hibernate
-
configuration>

Hibernate Mapping

<?xml version="1.0"?>

<!DOCTYPE hibernate
-
mapping PUBLIC "
-
//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate
-
mapping
-
3.0.dtd">

<!
--

Generated Apr 30, 2012 9:17:25 PM by Hibernate Tools 3.2.1.GA
--
>

<hibernate
-
mapping>


<class name="model.Buku" table="buku" catalog="buku">


<id name="bukuId" type="java.lang.Integer">


<column name="buku_id" />


<generator class="identity" />


</id>


<property name="bukuJudul" type="string">


<column name="buku_judul" length="35" />


</property>


<property name="bukuPengarang" type="string">


<column name="buku_pengarang" length="50" />


</property>


<property name="bukuPubliser" type="string">


<column name="buku_publiser" length="50" />


</property>


<property name="bukuTahun" type="string">


<column name="buku_tahun" length="4" />


</property>


</class>

</hibernate
-
mapping>


The main contract here is the creation of Session
instances.


Usually an application has a single
SessionFactory

instance and threads servicing client requests obtain
Session instances from this factory.


A
factory for
Session


SessionFactory
(org.hibernate.SessionFactory)


The main runtime interface between a Java
application and Hibernate.


This is the central API class abstracting the notion of a
persistence service.


A short
-
lived object


Representing a conversation between the application
and the database


Wraps a JDBC connection


Factory for Transaction

Session
(org.hibernate.Session)

Thank You....