Se integra con conceptos de frameworks de persistencia
No requiere de un contenedor JavaEE
Puede ser testeado en un ambiente JavaSE
Define una interface para los proveedores
Entity
Diferencia con Session Beans
Tienen identidad (Primary Key)
Son persistentes, tienen estado
No son remótamente accesibles
Su ciclo de vida es independiente
Entity Classes
@Entity
Es un POJO
No extiende de ninguna clase o interface
No requiere implementar Serializable
Mapea a una tabla de base de datos
Debe tener un Primary Key
Acceso por getters y setters
Puede tener métodos con lógica de negocio
Características adicionales
The class must have a public or protected, no-argument constructor.
The class may have other constructors.
The class must not be declared final.
No methods or persistent instance variables must be declared final.
If an entity instance be passed by value as a detached object,
such as through a session bean's remote business interface,
the class must implement the Serializable interface.
Entities may extend both entity and non-entity classes,
and non-entity classes may extend entity classes.
Clients must access the entity's state through accessor or business methods.
Persistent Fields and Properties
Java primitive types
java.lang.String
Other serializable types including:
Wrappers of Java primitive types
java.math.BigInteger
java.math.BigDecimal
java.util.Date
java.util.Calendar
java.sql.Date
java.sql.Time
java.sql.TimeStamp
User-defined serializable types
byte[]
Byte[]
char[]
Character[]
Enumerated types
Other entities and/or collections of entities
Embeddable classes
Primary Key
Simple
@Id
Compuesto
@EmbeddedId
@IdClass
Tipos
Java primitive types
Java primitive wrapper types
java.lang.String
java.util.Date (the temporal type should be DATE)
java.sql.Date
No usar tipos de punto flotante
Multiplicidad
@OneToOne
@OneToMany
@ManyToOne
@ManyToMany
Lifecycle
@PrePersist
@PostPersist
@PreRemove
@PostRemove
@PreUpdate
@PostUpdate
@PostLoad
Puede ser declarado en una clase Listener
@EntityListeners
EntityManager
Asociado a un Persistence Context
Tipos
Container-Managed Entity Managers
El Persistence Context se propaga automáticamente
@PersistenceContext EntityManager em;
Application-Managed Entity Managers
El Persistence Context no se propaga
El ciclo de vida del EntityManager es manejado por la app