Hibernate - Consorzio FormaWeb

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

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

58 εμφανίσεις

Hibernate


What

is

Hibernate

?


An
Object
/
Relational

Mapping

(ORM)
framework

for

Java


Hibernate let's 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


Open Source


Principal

Author
:
Gavin

King


Other Major Figure: Christian Bauer


Almost a defacto standard ORM for Java


Current version 4.2




Mapping

Process


The
process

of

specifying

the
bindings

between

an

object

model

and a database
schema



Principal

mechanism

is

via XML
mapping

files



Defacto

file
name

extension

is
: .
hbm.xml



Multiple ways to set this up: a single file, one file per class. Best practice is
is

to use
one file per class, with each file placed next to its corresponding class file in the
package hierarchy, and loaded as a
resource





Mapping

Process


Entities


Basic

Properties


Components



Associations


Many
-
To
-
One


One
-
To
-
Many


Many
-
To
-
Many


Principal

mechanism

is

via XML
mapping

files


Inheritance

mapping


Modeling

with

interfaces





Hibernate

App


An
Hibernate

App

will

consist

of
:


One

or more
classes

(
POJOs
)
to

be

popoulated

from
/
copied

to

database
tables



A
mapping

file
for

each

named

classname.hbm.xml

mapping

each

to

the
database
table

it

corresponds

to



Configuration

files

such

as

hibernate.cfg.xml


etc


etc



Hibernate

configuration


An
example

of

the
hibernate.config.xml




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


<!DOCTYPE
hibernate
-
configuration

PUBLIC "
-
//
Hibernate
/
Hibernate

Configuration

DTD 3.0//EN"



"http://www.hibernate.org/
dtd
/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
://
localhost
:3306/db_gestione_corsi


</
property
>


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


<
property

name=
"
hibernate.connection.password
"></
property
>


<
property

name=
"
hibernate.dialect
">
org.hibernate.dialect.MySQLDialect
</
property
>


<property name=
"
show_sql
">true</property>




<
mapping

resource=
"
it
/
tcweb
/
config
/
hibernate
/
persona.hbm.xml
" />



<
mapping

resource=
"
it
/
tcweb
/
config
/
hibernate
/
utente.hbm.xml
" />


</
session
-
factory
>


</
hibernate
-
configuration
>



Hibernate

configuration


An
example

of

the
mapping

file
utente.hbm.xml

and MANY
-
TO
-
ONE relation



<?xml
version=
"1.0"?>


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



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


<
hibernate
-
mapping
>



<
class

name=
"
it.tcweb.model.dto.Utente
"
table=
"utente"
catalog=
"db_gestione_corsi">




<
id

name=
"
id
"
type=
"
java.lang.Long
">





<
column

name=
"ID" />





<
generator

class=
"
identity
" />




</
id
>




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





<column name=
"USERNAME" length="20" not
-
null="true" unique="true" />




</
property
>




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





<column name=
"PASSWORD" length="20" not
-
null="true" unique="false" />




</
property
>




<
property

name=
"profilo"
type=
"
string
">





<column name=
"PROFILO" length="20" not
-
null="true" unique="false" />




</
property
>




<many
-
to
-
one name=
"persona" class="
it.tcweb.model.dto.Persona
">





<column name=
"ID_PERSONA" not
-
null="true" />




</
many
-
to
-
one
>



</
class
>


</
hibernate
-
mapping
>

Hibernate

configuration


An
example

of

the
mapping

file
persona.hbm.xml

and ONE
-
TO
-
MANY relation



<?xml
version=
"1.0"?>


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


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

<
hibernate
-
mapping
>


<
class

name=
"
it.tcweb.model.dto.Persona
"
table=
"persona"
catalog=
"db_gestione_corsi">



<
id

name=
"
id
"
type=
"
java.lang.Long
">




<
column

name=
"ID" />




<
generator

class=
"
identity
" />



</
id
>



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




<column name=
"NOME" length="30" not
-
null="true" unique="false" />



</
property
>



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




<column name=
"COGNOME" length="30" not
-
null="true" unique="false" />



</
property
>



<
property

name=
"
dataNascita
"
type=
"
java.util.Date
">




<column name=
"
data_nascita
" length="30" not
-
null="true" unique="false" />



</
property
>



<set
name=
"utenti"
cascade=
"
save
-
update
,
delete
,
delete
-
orphan
"
table=
"utente"
inverse=
"
true
" >




<key column=
"ID_PERSONA" not
-
null="true" on
-
delete="cascade"/>




<
one
-
to
-
many

class=
"
it.tcweb.model.dto.Utente
" />



</set>



</
class
>


</
hibernate
-
mapping
>