Java Reference
In-Depth Information
em.remove(catalogBean);
}
}
Java Persistence API
The
Java
Persistence
API
(
JPA
) is the persistence component of EJB 3.0. "An EJB
3.0 entity is a lightweight persistent domain object." As discussed in the previous
section, the entity class is a POJO annotated with the
@Entity
annotation. The
relationship modeling annotations
@OneToOne
,
@OneToMany
,
@ManyToOne
, and
@ManyToMany
, are used for object/relational mapping of entity associations. EJB 3.0
specifies the object/relational mapping defaults for entity associations.
The annotations for object/relational mapping are defined in the
javax.
persistence
package. An entity instance is created with the
new
operator and
persisted using the
EntityManager
API. An
EntityManager
is injected into an entity
bean using the
@PersistenceContext
annotation:
@PersistenceContext
EntityManager em;
An entity instance is persisted using the
persist()
method:
CatalogBean catalogBean=new CatalogBean();
em.persist(catalogBean);
The
EntityManager
is also used to remove entity instances using the
remove()
method:
em.remove(catalogBean);
EntityManager
is also used to find entities by their primary key with the
find
method:
CatalogBean catalogbean=(CatalogBean)(em.find("CatalogBean",
catalogId));
The
@NamedQuery
annotation is used to specify a named query in the Java Persistence
Query language, which is an extension of EJB-QL. The Java Persistence Query
language further adds operations for bulk update and delete,
JOIN
operations,
GROUP BY
,
HAVING
, and subqueries, and also supports dynamic queries and named
parameters. Queries may also be specified in native SQL.
@NamedQuery(
name="findAllBlogsByName",
query="SELECT b FROM Blog b WHERE b.name LIKE :blogName"
)
Search WWH ::
Custom Search