Documente Academic
Documente Profesional
Documente Cultură
Introducere
Hibernate este o librarie obiectual-relationala pentru limbajul Java, oferind un framework pentru maparea dintre un model obiectual si o baza de date relationala. Hibernate rezolva problemele tehnice legate de accesul la baza de date in vederea obtinerii persistentei obiectelor prin punerea la dispozitia utilizatorului a unor functii care lucreaza direct cu obiectele Java, luand asupra lor realizarea comunicarii directe cu baza de date. Hibernate este un software open-source si este distribuit sub GNU Lesser General Public License.
Caracteristici principale
facilitati dirty checking daca unui obiect persistent i s-a modificat un membru, actualizarea in baza de date se va face doar pentru acel membru nu pentru tot obiectul;
Hibernate poate fi folosit atat in aplicatii Java de sine statatoare cat si in aplicatii Java EE care folosesc servere EJB sau EJB session beans.
Arhitectura
Hibernate foloseste baza de date si niste fisiere de configuratie pentru a oferi servicii de persistenta pentru obiectele Java.
Arhitectura si API
Hibernate API (jar-ul org.hibernate) pune la dispozitie doua clase principale pentru realizarea persistentei obiectelor : SessionFactory si Session.
Session
aceasta clasa contine maparile penrtu o singura baza de date; o aplicatie care lucreaza cu mai multe baze de date va avea mai multe clase SessionFactory; este thread-safe; este immutable (read-only); reprezinta o factory pentru obiectele de tip Session; reprezinta un client pentru ConnectionProvider (o factory pentru conexiuni JDBC).
un wrapper pentru conexiunea JDBC; o factory pentru obiecte de tip transaction; single-threaded;
obiectul nu este si nu a fost niciodata asociat cu un context persistent (o sesiune); nu are identitate persistenta (cheie primara asociata). obiectul este asociat cu un context persistent (o sesiune). are identitate persistenta (cheie primara asociata) si poate un tuplu corespunzator in baza de date; Hibernate ofera garantia ca identitatea persistenta(primary key) si identitatea Java(identificatorul de obiect Java) corespund.
Persistent
a fost inchis; are identitate persistenta (cheie primara asociata) si poate un tuplu corespunzator in baza de date. Hibernate nu ofera nici o garantie ca identitatea persistenta(primary key) si identitatea Java(identificatorul de obiect Java) corespund.
Configuratia
Configuratia programatic
membri privati (recomandat, nu obligatoriu pentru Hibernate); getteri si setteri pentru membrii privati care sa respecte conventiile JavaBean(recomandat, nu obligatoriu pentru Hibernate); constructor fara argumente(obligatoriu folosit de Hibernate la instantierea prin reflectie: Constructor.newInstace()); un identificator pentru obiecte, folosit de Hibernate cand obiectele sunt salvate (recomandat pentru a folosi toate serviciile oferite de hibernate). Setterul poate fi privat.
Fisierele *.hbm.xml
Orice clasa persistenta are nevoie de o asemenea mapare; Contine maparea dintre clasa Java si tabela din baza de date aferenta; Este situat in acelasi folder cu clasa corespunzatoare.
Tuplizers
org.hibernate.tuple.Tuplizer interfata care stie sa trasforme un tuplu din baza de date intr-un obiect Java si invers; doua dintre cele mai folosite interfete sunt :
org.hibernate.tuple.entity.EntityTuplizer org.hibernate.tuple.component.ComponentTuplizer
utila pentru a folosi o clasa custom. De exemplu in loc de HashMap se va folosi un CustomMap creat de utilizator;
org.hibernate.UserType org.hibernate.CompositeUserType
Exemple se pot gasi in org.hibernate.test.DoubleStringType: <property name="twoStrings" type="org.hibernate.test.DoubleStringType"> <column name="first_string"/> <column name="second_string"/> </property> Se pot defini si nume mai scurte pentru un anumit tip de date: <typedef class="com.mycompany.usertypes.DefaultValueIntegerType" name="default_zero"> </typedef> <property name="priority" type="default_zero"/>
XdocletMarkup
/** * @hibernate.class * table="CATS" */ public class Cat { private Long id; // identifier private Date birthdate; private Cat mother; private Set kittens /* * @hibernate.id * generator-class="native" * column="CAT_ID" */ public Long getId() { return id; } private void setId(Long id) { this.id=id; } .. }
Asocieri Hibernate
Asocieri unidirectionale
Asocieri bidirectionale
One to many / many to one One to one One to many / many to one One to one Many to many
Database:
create table Person ( personId bigint not null primary key, addressId bigint not null ) create table Address ( addressId bigint not null primary key )
unique="true" not-
Database:
create table Person ( personId bigint not null primary key, addressId bigint not null unique ) create table Address ( addressId bigint not null primary key )
<generator
Database:
create table Person ( personId bigint not null primary key ) create table Address ( addressId bigint not null primary key, personId bigint not null )
Database:
create table Person ( personId bigint not null primary key )create table PersonAddress ( personId not null, addressId bigint not null primary key ) create table Address ( addressId bigint not null primary key )
Database:
create table Person ( personId bigint not null primary key )create table PersonAddress ( personId bigint not null primary key, addressId bigint not null ) create table Address ( addressId bigint not null primary key )
Database:
create table Person ( personId bigint not null primary key ) create table PersonAddress ( personId bigint not null primary key, addressId bigint not null unique ) create table Address ( addressId bigint not null primary key )
Database:
create table Person ( personId bigint not null primary key ) create table PersonAddress ( personId bigint not null, addressId bigint not null, primary key (personId, addressId) ) create table Address ( addressId bigint not null primary key )
Database:
create table Person ( personId bigint not null primary key, addressId bigint not null ) create table Address ( addressId bigint not null primary key )
Database:
create table Person ( personId bigint not null primary key, addressId bigint not null unique ) create table Address ( addressId bigint not null primary key )
Database:
create table Person ( personId bigint not null primary key ) create table PersonAddress ( personId bigint not null, addressId bigint not null primary key ) create table Address ( addressId bigint not null primary key )
Database:
create table Person ( personId bigint not null primary key ) create table PersonAddress ( personId bigint not null primary key, addressId bigint not null unique ) create table Address ( addressId bigint not null primary key )
Database:
create table Person ( personId bigint not null primary key )create table PersonAddress ( personId bigint not null, addressId bigint not null, primary key (personId, addressId) ) create table Address ( addressId bigint not null primary key )
Aceste asocieri sunt extreme de rare, Hibernate acoperind aproape toate tipurile de asocieri posibile.
Lucrul cu obiecte
Salvarea obiectelor
save() persist()
Incarcarea obiectelor
Actualizarea obiectelor
session.createSQLQuery("SELECT ID, NAME, BIRTHDATE FROM CATS").addEntity(Cat.class); Query query = session.createSQLQuery("SELECT * FROM CATS WHERE NAME like ?").addEntity(Cat.class); List pusList = query.setString(0, "Pus%").list();
dupa un anumit numar de cereri catre baza de date; dupa org.hibernate.Transaction.commit(); Session.flush().
Este posibil totusi a se schimba comportamentul default al lui flush(), astfel incat sa aiba loc dupa :