Java Data Persistence Using Hibernate - People

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

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

56 εμφανίσεις

Java Data Persistence
Using Hibernate

Jack Gardner

October 2004

2

Overview


What is Hibernate


What is ORM


Create Example


Read Examples


Persistable Classes


Database Table


Mapping Class to Table


Configuration


Configuration Properties



Session


Transaction


Object Lifecycle


Query Options


Associations


References

3

What is Hibernate?


An object/relational mapping (ORM)
implementation


Open source


Development started late 2001

4

What is Object/Relational Mapping?


Provides a transparent bridge between
objects and database tables


Allows source code to work with objects and
their attributes vs. tables and columns


Eliminates need for most/all SQL


Eliminates use of query result sets


5

Create Example


// create a new object

Widget w =
new

Widget();

w.setName(
“WidgetA”
);

w.setValue(
10
);


// persist it

session.save(w);

6

Read Examples


// get a known widget

Widget w = (Widget) session.load(Widget.
class
,
“WidgetA”
);


// get all widgets

List widgets = session.find(
“from Widget”
);

7

Persistable Classes


Classes are simply JavaBeans


public class

Widget

{


private

String

name;


private

int

value;



public

Widget() {}


public

String

getName() {
return

name;}


public

void

setName(
String

s) {name = s;}


public

int

getValue() {
return

value;}


public

void

setValue(
int

i) {value = i;}

}


8

Database Table


Persistable classes have an associated table

9

Mapping Class to Table


widget.hbm.xml


<
hibernate
-
mapping
>


<
class

name
=
“mypackage.Widget”

table
=
“WIDGET”
>



<
id

name
=
“name”

column
=
“NAME”
>




<
generator

class
=
“assigned”
/>



</
id
>



<
property

name
=
“value”

column
=
“VALUE”
/>


</
class
>

</
hibernate
-
mapping
>



10

Configuration


import

net.sf.hibernate.cfg.Configuration;

import

net.sf.hibernate.SessionFactory;


// build configuration based on properties

Configuration config =
new

Configuration();


// add persistable classes to configuration

config.addClass(Widget.
class
);


// build a session factory based on configuration

SessionFactory sessionFactory =
config.buildSessionFactory();


11

Configuration Properties


hibernate.properties


hibernate.dialect
=net.sf.hibernate.dialect.Oracle9Dialect

hibernate.connection.driver_class
=oracle.jdbc.driver.OracleDriver

hibernate.connection.url
=jdbc:oracle:thin:@oracle.cis.ksu.edu:1521:ORACLE

hibernate.connection.username
=<myusername>

hibernate.connection.password
=<mypassword>

12

Session


Lightweight and inexpensive to create/destroy


Not threadsafe


each thread needs its own


Obtained from SessionFactory instance


Session session = sessionFactory.openSession();

// perform persistence operations

session.close();

13

Transaction


Set of operations that are all committed or all
rolled back


Obtained from Session instance


Can perform multiple transactions within
session


Transaction tx = session.beginTransaction();

// perform persistence operations

tx.commit();


14

Object Lifecycle


Transient


Newly created object


Persistent


New object has been “saved”


Previously saved object has been “read”


Detached


Session closed


Persistent object serialized


Can be reattached later to become persistent



15

Query Options


Hibernate Query Language (HQL)


Query q = session.createQuery(
“from Widget w where

w.value > :value”
);


q.setParameter(
“value”
, someValue);


List widgets = q.list();



Criteria API


Criteria c = session.createCriteria(Widget.
class
);


c.add(Expression.gt(
“value”
, someValue);


List widgets = c.list();



SQL


Query q = session.createSQLQuery(
“select {w.*} from

WIDGET {w} where VALUE > :value”
,

“w”
,

Widget.
class
);


q.setParameter(
“value”
, someValue);


List widgets = q.list();


16

Associations


Supports all association types


one
-
to
-
one


one
-
to
-
many


many
-
to
-
many


Inherently unidirectional


Supports bidirectional

17

One
-
to
-
Many Example:
Unidirectional


Option 1: Group


Member



public class

Group



public class

Member


{





{



private

String

name;




private

String

name;



private

Collection members;













}


}



Option 2: Group


Member



public class

Group



public class

Member


{





{



private

String

name;




private

String

name;










private

Group group;


}














}

18

One
-
to
-
Many Example:
Bidirectional


Group


Member



public class

Group



public class

Member


{





{



private

String

name;




private

String

name;



private

Collection members;



private

Group group;













}





}



Application responsible for maintaining each
end of association

19

One
-
to
-
Many Table Structure


Underlying table structure not affected by
directionality


Standard implementation


Foreign key in many
-
side table


Alternate implementation


Use join table, with no
-
duplicate constraint on
many
-
side foreign key

20

One
-
to
-
Many Table Structure

21

Many
-
to
-
Many Example:
Unidirectional


Option 1: Group


Member



public class

Group



public class

Member


{





{



private

String

name;




private

String

name;



private

Collection members;













}


}



Option 2: Group


Member



public class

Group



public class

Member


{





{



private

String

name;




private

String

name;










private

Collection groups;


}














}

22

Many
-
to
-
Many Example:
Bidirectional


Group


Member



public class

Group



public class

Member


{





{




private

String

name;




private

String

name;




private

Collection members;



private

Collection groups;














}





}



Application responsible for maintaining each
end of association


23

Many
-
to
-
Many Table Structure


Underlying table structure not affected by
directionality


Implemented using join table

24

Many
-
to
-
Many Table Structure

25

References


Bauer, Christian and Gaven King,
Hibernate
in Action
, Manning, 2005.


Hibernate Reference Documentation
, Version
2.1.6, Hibernate, 2004.