2. WHAT IS JPA2
Java Persistence API
JPA2 in Java EE 6 (JPA1 in Java EE 5)
It’s a O/RM (Object Relational Mapping)
Framework to provide persistence
An object is a row in a master table (...)
mercoledì 13 febbraio 13
3. AND...
annotation || XML descriptor (configuration by exception)
POJO (Plain Old Java Object) based
Outside container (no server required)
No Bean interface required (no implements Serializable)
Different persistence providers (Hibernate, EclipseLink, ...)
mercoledì 13 febbraio 13
4. CASE STUDY: LIBRARY
The library contains several books
The library can loan one or more books to a person
Actors: Library, Book, Person, Loan
mercoledì 13 febbraio 13
5. O/RM
public class Person {
private Long id; PERSON
private String name;
PK ID
private String surname;
private Date birthday;
? NAME
private Collection<Loan> loanCollection;
SURNAME
public Person() {} GENRE
/* accessor methods */
} LOAN
public class Loan { PK ID
private Long id; FROMDATE
private Date fromdate;
private Date todate; TODATE
private Person person; FK PERSON
private Book book;
FK BOOK
public Loan(){ }
}
BOOK
public class Book { PK ID
private Long id;
private String title; TITLE
private String description; DESCRIPTION
public Book(){ } GENRE
}
mercoledì 13 febbraio 13
6. O/RM
to map variables to database columns
to handle data types between database and Java
to map tables references in relationship between objects
to manage the cycle of life of the object
mercoledì 13 febbraio 13
15. UPDATE
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Person> cq = cb.createQuery(Person.class);
Root from = cq.from(Person.class);
cq = cq.where(cb.and(cb.equal(from.get(Person_.name), "Tiziano"),
cb.equal(from.get(Person_.surname), "Lattisi")));
TypedQuery<Person> tq = em.createQuery(cq);
Person person = tq.getSingleResult();
assert "Tiziano".equals(person.getName()); assert person.getEmail() == null;
Long key = person.getId();
person.setEmail("tiziano.lattisi@gmail.com");
Person personByKey = em.find(Person.class, key);
assert "tiziano.lattisi@gmail.com".equals(personByKey.getEmail());
em.detach(person);
person = em.find(Person.class, key);
assert ! "tiziano.lattisi@gmail.com".equals(person.getEmail());
person.setEmail("tiziano.lattisi@gmail.com");
em.getTransaction().begin(); em.merge(person); em.getTransaction().commit();
Person personByKey2 = em.find(Person.class, key);
assert "tiziano.lattisi@gmail.com".equals(personByKey2.getEmail());
mercoledì 13 febbraio 13
16. DELETE
Person person = new Person();
person("Tiziano");
person("Lattisi");
em.getTransaction().begin();
em.persist(person);
em.getTransaction().commit();
Long aPrimaryKey = person.getId();
[...]
Person person = em.find(Person.class, aPrimaryKey);
em.getTransaction().begin();
em.remove(person);
em.getTransaction().commit();
person = em.find(Person.class, aPrimaryKey);
assert person == null;
mercoledì 13 febbraio 13
17. AND NOW...
Thank you.
Tiziano Lattisi
Me: http://www.linkedin.com/in/tizianolattisi/
Slides: http://www.slideshare.net/lattisi/
Examples: http://tinyurl.com/crn3wwq
mercoledì 13 febbraio 13