The New Enterprise Persistence Standard
The New Enterprise Persistence Standard
Mike Keith
[email protected]
http://otn.oracle.com/ejb3
About Me
@Entity
public class Employee { … }
<entity class=“com.acme.Employee”/>
The Minimal Entity
@Entity
public class Employee {
@Id int id;
EntityManager
MyEntity A
MyEntity C
MyEntity a MyEntity B
MyEntity b
Entities
Entity
state
Entity Manager
• Client-visible artifact for operating on entities
• API for all the basic persistence operations
• Can think of it as a proxy to a persistence
context
• May access multiple different persistence contexts
throughout its lifetime
• Multi-dimensionality leads to different aspects
of EntityManager (and persistence context)
naming
• Transaction type, life cycle
Operations on Entities
• EntityManager API
¾ persist()- Insert the state of an entity into the db
¾ remove()- Delete the entity state from the db
¾ refresh()- Reload the entity state from the db
¾ merge()- Synchronize the state of detached entity with the pc
¾ find()- Execute a simple PK query
¾ createQuery()- Create query instance using dynamic JP QL
¾ createNamedQuery()- Create instance for a predefined query
¾ createNativeQuery()-Create instance for an SQL query
¾ contains()- Determine if entity is managed by pc
¾ flush()- Force synchronization of pc to database
persist()
• Insert a new entity instance into the database
• Save the persistent state of the entity and any
owned relationship references
• Entity instance becomes managed
@Entity
public class Customer {
CUSTOMER
@Id ID NAME CREDIT PHOTO
int id;
String name;
@Column(name=“CREDIT”)
int c_rating;
@Lob
Image photo;
}
Simple Mappings
<entity class=“com.acme.Customer”>
<attributes>
<id name=“id”/>
<basic name=“c_rating”>
<column name=“CREDIT”/>
</basic>
<basic name=“photo”><lob/></basic>
</attributes>
</entity>
Relationship Mappings
• Common relationship mappings supported
¾ @ManyToOne, @OneToOne—single entity
¾ @OneToMany, @ManyToMany—collection of entities
• Unidirectional or bidirectional
• Owning and inverse sides of every bidirectional
relationship
• Owning side specifies the physical mapping
¾ @JoinColumn to specify foreign key column
¾ @JoinTable decouples physical relationship mappings
from entity tables
ManyToOne Mapping
@Entity
public class Sale {
SALE
@Id
int id; ID . . . CUST_ID
...
@ManyToOne CUSTOMER
Customer cust;
} ID ...
}
ManyToOne Mapping
<entity class=“com.acme.Sale”>
<attributes>
<id name=“id”/>
...
<many-to-one name=“cust”/>
</attributes>
</entity>
OneToMany Mapping
@Entity
public class Customer {
CUSTOMER
@Id
int id; ID ...
...
@OneToMany(mappedBy=“cust”)
Set<Sale> sales;
}
@Entity SALE
public class Sale {
ID ... CUST_ID
@Id
int id;
...
@ManyToOne
Customer cust;
}
OneToMany Mapping
<entity class=“com.acme.Customer”>
<attributes>
<id name=“id”/>
...
<one-to-many name=“sales” mapped-by=“cust”/>
</attributes>
</entity>
Persistence in Java SE
• No deployment phase
• Application must use a “Bootstrap API” to
obtain an EntityManagerFactory
• Resource-local EntityManagers
• Application uses a local EntityTransaction
obtained from the EntityManager
• New application-managed persistence context for
each and every EntityManager
• No propagation of persistence contexts
Entity Transactions
• Only used by Resource-local EntityManagers
• Isolated from transactions in other
EntityManagers
• Transaction demarcation under explicit
application control using EntityTransaction API
¾begin(), commit(), rollback(), isActive()
• Underlying (JDBC) resources allocated by
EntityManager as required
Bootstrap Classes
javax.persistence.Persistence
• Root class for bootstrapping an EntityManager
• Locates provider service for a named persistence unit
• Invokes on the provider to obtain an
EntityManagerFactory
javax.persistence.EntityManagerFactory