An Introduction to JPA Mapping - Arnaud Nauwynck

Java Persistence API declaration part. @Entity ... This document: http://arnaud.nauwynck.chez-alice.fr/CoursIUT/Intro-JPA-mapping.pdf ... JPA overview. ○ Main JPA Annotations. ○ Sample ... Standard tutorial .. easy to memorize. ○ Table ...
853KB taille 7 téléchargements 422 vues
IUT Paris8– January 2012

An Introduction to JPA Mapping Java Persistence API declaration part @Entity, persistence.xml, Spring …

Arnaud Nauwynck This document: http://arnaud.nauwynck.chez-alice.fr/CoursIUT/Intro-JPA-mapping.pdf

Table Of Content ●





Sample Relational model ●

Employee – Department tables schema



CRUD, SQL, relational model vs OO

JPA overview ●

Main JPA Annotations



Sample JPA Code

Configuring JPA ●

Maven + Code



Container Spring / XA / Pool configuration

Sample Db Table Schema ●

Standard tutorial .. easy to memorize ●

Table = Employee, Department, …



Relations = Emp->has manager, Emp->in Dept, ...

EMP ID

DEPT

(PK) number

LAST_MODIF

timestamp

NAME varchar DEPT_IT (FK) number MANAGER_ID (FK) number

ID

(PK) number

LAST_MODIF

timestamp

NAME varchar ADMIN_ID (FK) number

Sample SQL - CRUD ●

CRUD = Create-Read-Update-Delete



Create

INSERT Emp (id,name) VALUES(?,?)



Read

SELECT e.* FROM emp e WHERE e.id = ?





Update UPDATE Emp e SET e.salary = ? WHERE e.ID = ? Delete

DELETE Emp e WHERE e.id = ?

… DON'T forget COMMIT

SQL to Java ... ●

SQL is NOT procedural ●





NOT well typed ●

no bool, enum, type encapsulation, type constraints



Can mix EMP_ID and CAROT_ID

NO Algorithm structures ●



Pointer, List, Map, Set, Hash, Array …

NOT Object-Oriented ●



PL-SL oracle extension for “if”, “for”, “procedure”...

class, method overload

NO Library, IDE, skilled developers

Whereas Java …

CRUD code in JPA ●

Create



Read ●

By Id



By QL



By Criteria



Update



Delete

ORM : Object to Relational Mapping

ORM Translation Dictionary PersistenceUnit Class Object Instance Field Id Pointer Address List Sub-class hierarchy

Schema Table Row Column Primary Key FK col (index uniq scan) Rowid ~ Table join by FK to PK

~ Table join by ID

JPA Emp Class

JPA EmpDAO

Convinced? … Upgrade to JPA ●

Step 1 : add Maven dependency ●

● ●



Javax.persistence + hibernate (scope=runtime)

Step 2 : add @Entity, @Id … Step 3 : remove Jdbc code … use EntityManager DAO code Step 4: configure Container (Spring, ..)

API + Vendor Implementation Hibernate

add META-INF/persistence.xml

Spring Xml Configuration + Scan + IOC Magic

Spring @Configuration for DataSource + Hibernate

Other Xml Configuration Sample (EclipseLink + ${} db properties)

EmpDAO Junit

Test

… Noticed “no” Getter-Setter !! Because Small is Beautifull (http://projectlombok.org)

ANNEXE : Details on JPA Annotations @Entity, @Id, @Version @GeneratedValue, @SequenceGenerator @ManyToOne, @ManyToMany ...

Core : @Entity + @Id, @Version ●



Minimal mapping : @Entity Need to specify an @Id !! (object is an entity If-and-Only-If it has a unique Id) ●

Example: Emp => generated Id, or Social Security Number..



Better with @Version … for optimisitc-lock / cache...



All OTHERS annotations are OPTIONNALS



By default, ●

Table name = shortname of class



All fields are persistent... column name = field name

Entity Custom mapping ●

Override Table name or entity-table mapping... optionnal





@Table()

Override field mapping optional @Column() Override Id generation

@GeneratedValue @SequenceGenerator

@Table

@Column

@GeneratedValue, @SequenceGenerator ●



Sequences are DB-vendor specific ●

Hsqldb … “select max(ID) + 1” on insert



Postresql, Mysql … auto-incremented column



Oracle: use sequence !

Sample SQL with Oracle sequence CREATE SEQUENCE seq_emp … Newid = … SELECT seq_emp.next FROM dual INSERT EMP (ID, name...) VALUES (:NewID, ...)

@GeneratedValue, @SequenceGenerator

JPA ref name

DB seq name

Relation @ManyToOne + optional @JoinColumn (= Pointer / Foreign Key to PK) Emp id deptId

0..1

Department id

@OneToMany (= List / where Foreign Key=PK) Emp id

EmpInfo

1 0..*

Id user_id

@Inheritance, @DiscriminatorValue @DiscriminatorColumn

EmpInfo

PhoneEmpInfo

KeyValueEmpInfo

Questions ? Alors TP ! This document: http://arnaud.nauwynck.chez-alice.fr/CoursIUT/Intro-JPA-mapping.pdf

Download zipped TP code http://arnaud.nauwynck.chez-alice.fr/CoursIUT/test-jpa.zip