Exercise: Hibernate part 1

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

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

76 εμφανίσεις

COURSE
:
WEBP




28. September 2009 / Henrik Høltzer



Exercise: Hibernate


part 1


A)

Create a new MySQL DB

Create a new DB (e.g. in MySQL) containing
the following three

tables:



CUSTOMER (id, name, phone)



ARRANGEMENT(id, name, description, date
)



ENROLMENT(number, date)


B
)

Hibernate support

With a point of reference in the tutorial: “Using Hibernate in Java

Swing Application


(
http://www.netbeans.org/kb/docs/java/hibernat
e
-
java
-
se.html

) you should create a new NetBeans project and
create an access to the database with Hibernate through the following steps:

1.

Add Hibernate support to the project

2.

Create HibernateUtil.java

3.

Create Hibernate reverse engineering mapping file

4.

Cre
ate Hibernate Mapping files and POJO classes from the DB

5.

Create a simple main and test
that you got a connection to the DB

6.

Crea
te a simple Application GUI so you can create and get Customers
,
Arrangements and Enrolments

Use the query
-
browser to verify the
updates in the DB.



C) Customer (Entity
-
Type)


Address (Value
-
Type) (cardinality: 1
-
1)

In this case we want a special class Address (for reuse?), but an address doesn’t have a unique id and is totally
dependent of the Customer so:


Expand the CUSTOMER ta
ble with Address information

(street, number, city, zipcode, contry ..). Follow step 1
-
5 from
B). Create a new class Address and adjust the mapping
-
files.


Hint/Tip:

Use <component> (look
at
chap. 4 p. 185 in “Java Persistence with Hibernate”). Notice


th
ere shouldn’t be
any mapping
-
file for the class Address since we don’t have any ADDRESS table (Value
-
Types don’t have there own PK)

Remarks:


A simple test


use the main( )
-
method to create a Customer object and an associated Address object. Make the
obje
cts persistent using the save( ) method (
from class Session


you get the Session
-
object with the
getSessionFactory( ).openSession( ) method)
, commit the transaction, close the Session and open a new Session, use
the get( ) method to retrieve the object ag
ain (verify under the fly
,

with the Query
-
browser
,

that the DB is updated)



D
) Customer (Entity
-
Type)


Address (
Entity
-
Type) (cardinality: 1
-
1)

In this case an Address is unique (even that to customer
s

can’t share the same address!)
, but maybe we later w
ant to
associate an Arrangement with an address


so:

Make a new DB with the tables CUSTOMER and ADDRESS and follow the steps 1
-
5 from B).

Adjust the mapping
-
files
and consider both uni
-

and bi
-
directional associations.

Hint/Tip:

(look at chap. 7 p. 283
-
284 in “Java Persistence with Hibernate”)



E
) Customer


Enrolment (Cardinality: 1
-
*)
A
rrangement
-

Enrolment

(cardinality:
1
-
*
)

Expand the DB with the tables: ARRANGEMENT and ENROLEMENT. Make some matching mapping
-
files;

consider both
uni
-

and bi
-
direct
ional associations
.



Hint/Tip:

(look at chap. 6 p. 262 in “Java Persistence with Hibernate”)


F
)
Annotations

Make a copy of the projects from exercise D) and E), erase the XML
-
mapping
-
files and add annotations in classes.
(Alternative: make copies of the
classes and add the annotations here)



G) HQL/SQL

Use HQL to make some queries in DB (e.g. find all
the
customer
s

with a name that’s starts with an “A”).