Java Reference
In-Depth Information
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:
EntityManager em;
An entity instance is persisted using the persist() method:
CatalogBean catalogBean=new CatalogBean();
The EntityManager is also used to remove entity instances using the
remove() method:
EntityManager is also used to find entities by their primary key with the
find method:
CatalogBean catalogbean=(CatalogBean)(em.find("CatalogBean",
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.
query="SELECT b FROM Blog b WHERE LIKE :blogName"
Search WWH ::

Custom Search