Cover | Table of Contents
Web services are network applications that use SOAP and WSDL to exchange information in the form of XML documents.
@javax.persistence.Entity annotation and have at least one field
or getter method that is designated as the primary key of the
entity bean class in the database. This is usually done with the
@javax.persistence.Id annotation. Entities have other
annotations available to define a full object-relational
database mapping as well.@javax.annotation.PostConstruct on the EJB's
bean class if one is provided. This call gives the bean instance an
opportunity to do any additional initialization before the EJB
services any request. Other callback methods can be used by the bean
class in a similar fashion. EJB defines when these various callback
methods are invoked and what can be done within their contexts.EntityManager service.EJBContext , and the JNDI environment-naming context.
The callback methods notify the bean class that it is involved in a
life cycle event. The EntityManager service. These
artifacts define a common model for distributed server-side components
as well as a persistence model that can be used on the server or in
standalone applications. But this model isn't enough to make EJB
interesting or even particularly useful. EJB servers also manage the
resources used by beans, and can manage thousands or even millions of
distributed objects simultaneously. They must manage how distributed
objects use memory, threads, database connections, processing power, and
more. Furthermore, the EJB specification defines interfaces that help
developers take advantage of these common practices.synchronized keyword. Prohibiting the
use of the thread synchronization primitives prevents developers
from thinking that they control synchronization and enhances the
performance of bean instances at runtime. In addition, the EJB
specification explicitly prohibits beans from creating their own
threads. In other words, as a bean developer, you cannot create a
thread within a bean. The EJB container has to maintain complete
control over the bean in order to properly manage concurrency,
transactions, and persistence. Allowing the bean developer to
create arbitrary threads would compromise the container's ability
to track what the bean is doing and would make it impossible for
the container to manage the primary services.package com.titan.domain
import javax.persistence.*;
@Entity
@Table(name="CABIN")
public class Cabin implements java.io.Serializable{
private int id;
private String name;
private int deckLevel;
private int shipId;
private int bedCount;
@Id
@Column(name="ID")
public int getId( ) { return id; }
public void setId(int pk) { id = pk; }
@Column(name="NAME")
public String getName( ) { return name; }
public void setName(String str) {name = str; }
@Column(name="DECK_LEVEL")
public int getDeckLevel( ) { return deckLevel; }
public void setDeckLevel(int level) { deckLevel = level; }
@Column(name="SHIP_ID")
public int getShipId( ) { return shipId; }
public void setShipId(int sid) { shipId = sid; }
@Column(name="BED_COUNT")
public int getBedCount( ) { return bedCount; }
public void setBedCount(int bed) { bedCount = bed; }
}package com.titan.domain
import javax.persistence.*;
@Entity
@Table(name="CABIN")
public class Cabin implements java.io.Serializable{
private int id;
private String name;
private int deckLevel;
private int shipId;
private int bedCount;
@Id
@Column(name="ID")
public int getId( ) { return id; }
public void setId(int pk) { id = pk; }
@Column(name="NAME")
public String getName( ) { return name; }
public void setName(String str) {name = str; }
@Column(name="DECK_LEVEL")
public int getDeckLevel( ) { return deckLevel; }
public void setDeckLevel(int level) { deckLevel = level; }
@Column(name="SHIP_ID")
public int getShipId( ) { return shipId; }
public void setShipId(int sid) { shipId = sid; }
@Column(name="BED_COUNT")
public int getBedCount( ) { return bedCount; }
public void setBedCount(int bed) { bedCount = bed; }
}Cabin bean class is
annotated with @javax.persistence.Entity and @javax.persistence.Table . The @Entity annotation tells the persistence provider
that this is an entity class that is mapped to a database and that
can be managed by an createCabin( ) and
findCabin( ) methods to
manipulate Cabin entities:package com.titan.travelagent;
import javax.ejb.Remote;
import com.titan.domain.Cabin;
@Remote
public interface TravelAgentRemote {
public void createCabin(Cabin cabin);
public Cabin findCabin(int id);
}@javax.ejb.Remote annotation. This annotation tells the EJB
container that this particular interface is the remote business
interface of the TravelAgent EJB. Unlike EJB 2.1, also notice that
the business methods do not have to throw java.rmi.RemoteException . They can if they want to, but they don't
have to.@javax.ejb.Stateless annotation to denote this. Although they
are not required to, it is good practice for stateless session beans
to implement all of their business interfaces so that the
client/bean contract can be enforced by the Java compiler. In this
case, the business interface is TravelAgentRemote . Here is the complete definition of the TravelAgentBean class:package com.titan.travelagent;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import com.titan.domain.Cabin;
@Stateless
public class TravelAgentBean implements TravelAgentRemote{
@PersistenceContext
(unitName="titan")
private EntityManager manager;
public void createCabin(Cabin cabin) {
manager.persist(cabin);
}
public Cabin findCabin(int pKey) {
return manager.find(Cabin.class, pKey);
}
}Home interface. So, how are entity beans created?
How are they updated and removed? How can you perform queries and such?
All of these persistent actions are now performed through the javax.persistence.EntityManager service.EntityManager is the central service for all
persistence actions. Entities are plain Java objects that are allocated
just like any other Java object. They do not become persistent until
your code explicitly interacts with the EntityManager to make them persistent. The
EntityManager manages the O/R
mapping between a fixed set of entity classes and an underlying
data source. It provides APIs for creating queries, finding objects,
synchronizing objects, and inserting objects into the database. It also
can provide caching and manage the interaction between an entity and
transactional services in a Java EE environment such as JTA. The
EntityManager is tightly integrated
with Java EE and EJB but is not limited to this environment; it can be
used in plain Java programs.new( ) operator just as you would any other
plain Java object. Instances of an entity bean class do not become
persistent until they are associated with an EntityManager. For instance, let's look at a
simple example of a Customer entity:import javax.persistence.*;
@Entity
public class Customer {
private int id;
private String name;
@Id @GeneratedValue
public int getId( ) {
return id;
}
public void setId(int id) {
this.id = id;
}
String getName( ) {
return name;
}
public void setName(String name) {
this.name = name;
}
}Customer class, no magic happens when
new( ) is invoked. Calling the
new operator does not magically
interact with some underlying service to create the Customer class in
the database:Customer cust = new Customer( );
cust.setName("Bill");EntityManager to create the
entity in the database.EntityManager, the manager tracks state
changes to the entity and synchronizes those changes to the database
whenever the entity manager decides to flush its state. When an entity
is detached, it is unmanaged. Any state changes to an entity that is
detached are not tracked by the entity manager.EntityManager maps
a fixed set of classes to a particular database. This set of classes
is called a persistence unit . Before you can even think about creating
or querying entities with an entity manager, you must learn how to
package a persistence unit for use within a Java SE (regular Java
application) or Java EE (application server) environment. A
persistence unit is defined in a persistence.xml
file. This file is a required deployment descriptor for the Java
Persistence specification. A persistence.xml file
can define one or more persistence units. This file is located in the
META-INF directory of:
@javax.persistence.Entity annotation and, if it is, it will add it to
the set of entities that must be mapped.EntityManager so that you can persist,
update, remove, and query your entity beans within your databases. In
Java SE, entity managers are created using a javax.persistence.EntityManagerFactory . Although you can use the factory interface
in Java EE, this platform provides some additional features that make
it easier and less verbose to manage entity manager instances.EntityManagers may be
created or obtained from an EntityManagerFactory. In a Java SE
application, you must use an EntityManagerFactory to create instances
of an EntityManager. Using the
factory isn't a requirement in Java EE.package javax.persistence;
public interface EntityManagerFactory {
EntityManager createEntityManager( );
EntityManager createEntityManager(java.util.Map map);
void close( );
boolean isOpen( );
}createEntityManager( )
methods return EntityManager
instances that manage a distinct extended persistence context. You
can pass in a java.util.Map
parameter to override or extend any provider-specific properties you
did not declare in your persistence.xml file.
When you are finished using the EntityManagerFactory, you should close( ) it (unless it is injected; ee'll
discuss this later). The isOpen(
) method allows you to check to see if the EntityManagerFactory reference is still
valid.javax.persistence.Persistence class is responsible for bootstrapping
an EntityManagerFactory:public class Persistence {
public static EntityManagerFactory createEntityManagerFactory(
String unitName
);
public static EntityManagerFactory createEntityManagerFactory(
String unitName,
java.util.Map
properties
);javax.persistence.Persistence class
looks for persistence.xml deployment
descriptors within your Java classpath. The