Hibernate

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

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

80 εμφανίσεις

Hibernate
by Dan Kigelman
An object-relational mapping tool for Java
Scenario:

Too many books, too few readers

Want to share with friends (mock­library)

Need to keep track of:

title

author

description

available?

current location
It starts with an object
Direct Java JDBC Approach:

Imagine:

more classes

more methods

Tedious

Repetative

Error­prone

DB­specific
Pros:
Cons:

Very flexible

Direct access to 
SQL
Object­relational mapping (ORM)

Maps java objects to database


tendency towards enlightened laziness”
http://www.onjava.com/pub/a/onjava/2005/09/21/what­is­hibernate.html

Saves time and sanity

Usually at the loss of flexibility

(ORM controversy)

Many ORM tools exist
Hibernate features

Unobtrusive: few changes to java classes

We keep our Plain Old Java Objects

Class must have no­parameter constructor

Class must follow java naming convention for 
attributes, getters, and setters

Open­source (LGPL)

Vendor­independent:

Oracle, DB2, Sybase, MS SQL Server, PostgreSQL, MySQL, HypersonicSQL, Mckoi 
SQL, SAP DB, Interbase, Pointbase, Progress, FrontBase, Ingres, Informix, Firebird

Flexible – supports object association, 
inheritance, and polymorphism
Book.hbm.xml

This tells Hibernate 
which classes to 
make persistent

Mapping java class 
to db fields

Hibernate uses 
get/set methods for 
each property

Highly customizable
Hibernate.cfg.xml

Select DB Dialect
(currently hsql)

Creates / updates 
schema if needed
Simple Queries
Relations: objects as attributes
public class Friend {
private String name;
private String address;
private Book[] books;
...
}
public class Book {
private Long id;
private String title;
...
private Friend currentReader;
...
}

Supports one­to­many, 
many­to­many 
relationships

Classes behave naturally

Changes made to:

Book.hbm.xml

Friend.hbm.xml
Advanced Queries

Supports both HQL (Hibernate Query 
Language) and SQL

Example:
select count(payment), status.name 
from Payment as payment 
    join payment.currentStatus as status
    join payment.statusChanges as statusChange
where payment.status.name <> PaymentStatus.AWAITING_APPROVAL
    or (
        statusChange.timeStamp = ( 
            select max(change.timeStamp) 
            from PaymentStatusChange change 
            where change.payment = payment
        )
        and statusChange.user <> :currentUser
    )
group by status.name, status.sortOrder
order by status.sortOrder
Resources and References

Hibernate.org

Getting started quickly:
http://www.hibernate.org/152.html

Tutorial:
http://www.hibernate.org/hib_docs/v3/reference/en/html/tutorial.html

Eloquent overview: 
http://www.onjava.com/pub/a/onjava/2005/09/21/what­is­hibernate.html

Another tutorial:
http://www.onjava.com/pub/a/onjava/2004/01/14/hibernate.html

Wikipedia

http://en.wikipedia.org/wiki/Object­relational_mapping#Object­Relational_mapping_utilities

http://en.wikipedia.org/wiki/Hibernate_%28Java%29

HypersonicSQL ­ 100% Java Database (open­source)

http://www.hsqldb.org