Assignment 7 Persistence with Hibernate

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

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

83 εμφανίσεις

http://
www.aop.informatik.tu-darmstadt.de
/
Assignment

7
Persistence

with

Hibernate
© 2007
Aspect-Oriented Programming
Group - Prof. Dr. Klaus Ostermann
Department of Computer Science
Darmstadt University of Technology
Dr.-
Ing
. Michael
Eichberg
Dipl
.-Inform. Michael Achenbach
http://
www.aop.informatik.tu-darmstadt.de
/
2
Content

Objectives

Your Assignment

Library management application

Task 1 -
Hibernate Mapping using Annotations

Task 2 - Service methods

Task 3

Write
JUnit
tests

Overview over Hibernate

JUnit

Links
http://
www.aop.informatik.tu-darmstadt.de
/
3
Objectives

What you should learn in this assignment:

Get a first look into persistence

Get an overview of the Hibernate O/R mapping
framework

See what O/R
mappers
do for you

What you don

t learn

How to create databases in production quality
(foreign key references, indices, etc.)

Some
of
the

more

advanced
Hibernate
features
http://
www.aop.informatik.tu-darmstadt.de
/
4
Your assignment

We

ll write a library management application

Use the following conceptual data model
Author
firstName
: String
lastName
: String
Book
pages :
int
release : Date
title : String
Person
firstName
: String
lastName
: String
email : String
Employee
login : String
password : String
Student
matNumber
: String
n
m
n
0..1
http://
www.aop.informatik.tu-darmstadt.de
/
5
Task 1 (4 Pt)

Add mapping information using Java
annotations

Prefer Java annotations over O/R mapping
configuration via XML

Configure via API not via
hibernate.cfg.xml

Use

sequence

as primary key generator

For inheritance mapping, use the table-per-
subclass strategy (see lecture slides)
http://
www.aop.informatik.tu-darmstadt.de
/
6
Task 2 (3 Pt)

Implement a

LibraryService

facade

hibernate.HibernateLibraryService

Do not insert objects (for example authors) that
are already in the database

use them instead

If we search for a book, we want to retrieve the
whole object

I.e., we should be able to access its authors or the guy who
borrows it
public interface
LibraryService
{
void
addBook(Book
book) throws Exception;
void
addEmployee(Employee
employee) throws Exception;
void
borrow(Person
person, Book
book
) throws Exception;
Book
searchByTitle(String
title) throws Exception;
void
addStudent(Student
student) throws Exception;

}
http://
www.aop.informatik.tu-darmstadt.de
/
7
Task 3 (3 Pt)

Write
JUnit
tests

Test Setup:

Add the books on the following slide to the library
system

Add the persons on slide 9 to the library system

Reflect that

Michael Achenbach

borrows the
book

AspectJ in Action

http://
www.aop.informatik.tu-darmstadt.de
/
8
Task 3 - continued

Books to add


AspectJ in Action

by
Ramnivas

Laddad
, 512 pages,
released at July 1st, 2003


Contributing to Eclipse

by Erich Gamma and Kent Beck,
395 pages, released at October 1st, 2003


Design Patterns

by Erich Gamma, Richard Helm, and
Ralph E. Johnson, 416 pages, released at July 1st, 1997


Patterns of Enterprise Application Architecture

by Martin
Fowler, 533 pages, released at November 1st, 2002
http://
www.aop.informatik.tu-darmstadt.de
/
9
Task 3 - continued

Persons to add

Employees (login and password is

123

)

Klaus
Ostermann

Michael
Eichberg

Michael Achenbach

Students (matriculation number is

123

)

Student One

Student Two

You can find the needed instances as static
fields in the corresponding classes!
http://
www.aop.informatik.tu-darmstadt.de
/
10
Task 3 - continued

Test Case 1:
Search the book with the title

Patterns of
Enterprise Application Architecture

and test if
the resulting authors are correct

Test Case 2:
Search the person

Michael Achenbach

and
test if the list of books he borrowed is correct

Test Case 3:
Search the book with the title

AspectJ
in
Action

and test if the borrower is correct
http://
www.aop.informatik.tu-darmstadt.de
/
11
Your assignment - continued

When are two objects equal?

Author: equal first and last name

Book: equal title

Person: equal first and last name

If you need to delete the database, you can
delete the database.* files in the working
directory (if you don

t see them, refresh your
project)

If you get stuck with XML-configuration (>
1h), post your problem to the mailing list
http://
www.aop.informatik.tu-darmstadt.de
/
12
Overview over Hibernate Annotations

Configuration using annotations

Hibernate property file (already implemented)

hibernate.cfg.xml
(
not

used
)

O/R mapping file (part of your task)

Java annotations (part of your task)

Putting it all together

Hibernate
Util
(
HibernateUtil.getSessionFactory
())

Configuration API

AnnotationConfiguration

config
=
new

AnnotationConfiguration
();

config.addAnnotatedClass
(<class with annotations>);

config.addResource
(<O/R mapping>);
http://
www.aop.informatik.tu-darmstadt.de
/
13
Overview over Hibernate Annotations

Annotations

@Entity

@Id

@
GeneratedValue

Consider the strategy

@
ManyToOne

@
ManyToMany

Consider the target entity

@
OneToMany

Consider the target entity

@Inheritance

Consider the strategy
http://
www.aop.informatik.tu-darmstadt.de
/
14
JUnit

Run the
JUnit
test by

right-clicking your test
case file

,

Run as

,

JUnit
Test


The
JUnit
view shows you the results

Use
JUnit
assertions to verify your results

assertEquals

assertTrue

Use the
setUp/tearDown
methods to fill/empty
your database

Add the test case implementations to the
given test* methods
http://
www.aop.informatik.tu-darmstadt.de
/
15
Links

Hibernate resources:

http://www.hibernate.org/

http://www.hibernate.org/hib_docs/annotations/ref
erence/en/html/

JUnit
resources:

http://
www.junit.org