文档介绍:e to
Enterprise Development
with the
Java Persistence API
Patrick Linskey Mike Keith
BEA Systems Oracle Corp.
Agenda
Enterprise entity models
Persistence context propagation
Data caching
Detaching and merging entities
Entities in enterprise transactions
Packaging and deploying in the container
Summary
Access Type
JPA supports two access types:
• PROPERTY
• FIELD
Tells the JPA persistence provider how to
access the data in your entity instances
Has nothing to do with encapsulation
Use FIELD access
***@Entity
public class Flight {
...
***@OneToMany public Carrier getCarrier() {return carrier;}
public void setCarrier(Carrier c) {
if (!(()))
throw new IllegalStateException(c +
“ does not fly into ”+ ());
else
carrier = c;
}
}
Inheritance
Entities can extend:
• Other entities
– Either concrete or abstract
• Mapped superclasses
– mon entity state
• Ordinary (non-entity) Java classes
– Supply behavior and/or non-persistent state
Mapped Superclass
***@MappedSuperclass public class Person {
***@Id protected Long id;
protected String name;
***@Embedded protected Address address;
}
***@Entity public class Customer extends Person {
***@Transient protected int orderCount;
***@OneToMany
protected Set<Order> orders = new HashSet();
}
***@Entity public class Employee extends Person {
***@ManyToOne
protected Department dept;
}
Abstract Entity
***@Entity public abstract class Person {
***@Id protected Long id;
protected String name;
***@Embedded protected Address address;
}
***@Entity public class Customer extends Person {
***@Transient protected int orderCount;
***@OneToMany
protected Set<Order> orders = new HashSet();
}
***@Entity public class Employee extends Person {
***@ManyToOne
protected Department dept;
}
Inheritance Mapping
Map inheritance hierarchies in three ways
1. SINGLE_TABLE (required)
2. JOINED (required)
3. TABLE_PER_CLASS (optional)
JPA sp