Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Talk Outline
• Object Relational Mapping
• Why JPA?
• JPA versions
• JPA interfaces
• Simple JPA example
• JPA Object life cycle
• JPA standalone - Spring
• ORM JPA 1.0 – JPA 2.0
• Java Persistence Query Language JPA 1.0 – JPA 2.0
• Criteria API JPA 2.0
• Concurrency
• Caching
Object relational mapping issues
Default table name = class private void setId(long id) { this.id = id; } public
String getName() { return name; } public void
name setName(String name) { this.name = name; }
Creating and saving
Registration Application
Managed Bean Entity Class
Item
DB
ItemController
Catalog
Spring
JSF Components Bean
Spring
Framework
Spring with JPA
Component Stereotype
@Repository Spring transactions use aop
@Transactional
public class CatalogDAO implements CatalogService {
@PersistenceContext(unitName="PetCatalogPu")
private EntityManager em;
@Transactional(readOnly=true)
public List<Item> getItems(int firstItem,int batchSize) {
Query q =
em.createQuery("select object(o) from Item as o");
q.setMaxResults(batchSize);
q.setFirstResult(firstItem);
List<Item> items= q.getResultList();
return items;
}
ORM Essentials JPA 1.0
• Entities
• Basic types
– Strings, integers, floats, decimals, …
• Embeddable classes
– E.g. Address
• Relationships
– One-to-one, one-to-many/many-to-one, many-to-many
– Collections modeled with java.util Collection, Set, List, or Map
– Customized via metadata: @JoinColumn, @JoinTable, etc.
• Inheritance
– Single table, joined subclass, table per class (optional)
Annotations versus XML
@Entity
public class Employee {
@Id private int id;
@Column(name="EMP_NAME")
private String name;
@Column(name="SAL")
private long salary;
// ...
}
orm.xml snippet:
<entity-mappings>
...
<entity class="examples.model.Employee"
metadata-complete="true">
<attributes>
<id name="id"/>
</attributes>
</entity>
...
</entity-mappings>
Associations
• JPA provides a rich set of alternatives for mapping object
associations in the database.
• Providing for all the various multiplicities of relationships.
– Many to one
– One to many
– Many to many
– One to one
• Providing for Java’s understanding of directionality.
– Unidirectional
– Bidirectional
NB Bidirection Many to one also One to Many
• Providing for fine grained control of “transitive persistence”
Transitive Persistence
• Controls when/how associated objects react to
changes in the other.
– For example, should associated address rows be deleted when a customer object is
removed (cascade delete).
– When an new order is saved should associated new line item objects also be saved
(cascade save).
Association Employee->Department
package examples.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Employee {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
private long salary;
@ManyToOne
private Department department;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Department {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
@OneToMany(mappedBy="department")
private Collection<Employee> employees;
public Department() {
employees = new ArrayList<Employee>();
}
public void addEmployee(Employee employee) {
if (!getEmployees().contains(employee)) {
getEmployees().add(employee);
if (employee.getDepartment() != null) {
employee.getDepartment().getEmployees().remove(employee);
}
employee.setDepartment(this);
}
}
TypedQuery<ResultType> tq = em.createQuery(cquery);
List<ResultType> result = tq.getResultList();
Criteria Query Example
SELECT distinct d
FROM Department d, Employee e
WHERE d = e.department
CriteriaQuery<Department> c = cb.createQuery(Department.class);
Root<Department> dept = c.from(Department.class);
Root<Employee> emp = c.from(Employee.class);
c.select(dept)
.distinct(true)
.where(cb.equal(dept, emp.get("department")));
TypedQuery<Department> q = em.createQuery(c);
List l = q.getResultList();
MetaModel – strong typed criteria queries
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Embedded;
import javax.persistence.OneToMany;
import java.util.Set;
@Entity
public class Customer {
@Id int custId;
@Embedded Address address;
@OneToMany Set<Order> orders;
…
}
Metamodel Class
package com.example;
import javax.annotation.Generated;
import javax.persistence.metamodel.SetAttribute;
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.StaticMetamodel;
• javax.persistence.lock.scope property
• javax.persistence.lock.timeout hint