Java Reference
In-Depth Information
@PersistenceContext
private
private EntityManager em ;
Our EJB class is annotated with the @javax.ejb.Stateless annotation to mark it as a state-
less session EJB. The CustomerResourceBean class implements the CustomerResource in-
terface.
There is a javax.persistence.EntityManager field named em . The annotation
@javax.persistence.PersistenceContext injects an instance of the EntityManager into
that field. The EntityManager persists Java objects into a relational database. These are all
facilities of EJB and JPA.
public
public Response createCustomer ( Customer customer , UriInfo uriInfo )
{
CustomerEntity entity = new
new CustomerEntity ();
domain2entity ( entity , customer );
em . persist ( entity );
em . flush ();
System . out . println ( "Created customer " + entity . getId ());
UriBuilder builder = uriInfo . getAbsolutePathBuilder ();
builder . path ( Integer . toString ( entity . getId ()));
return
return Response . created ( builder . build ()). build ();
}
The createCustomer() method implements the RESTful creation of a Customer in the
database. The Customer object is the unmarshalled representation of the XML document
posted through HTTP. The code allocates an instance of
com.restfully.shop.persistence.CustomerEntity and copies the data from Customer
to this instance. The EntityManager then persists the CustomerEntity instance into the
database. Finally, the method uses UriInfo.getAbsolutePathBuilder() to create a URL
that will populate the value of the Location header that is sent back with the HTTP re-
sponse.
public
public Customer getCustomer ( int
int id )
{
CustomerEntity customer = em . getReference ( CustomerEntity . class ,
id );
return
return entity2domain ( customer );
}
The getCustomer() method services GET /customers/< id > requests and retrieves Cus-
tomerEntity objects from the database using the EntityManager . The entity2domain()
Search WWH ::




Custom Search