Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package service; import javax.persistence.EntityManager; import model.Item; import java.net.URI; import java.util.List; import javax.persistence.Query; import javax.persistence.PersistenceContext; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.springframework.transaction.annotation.Transactional; /** * * @author Anthony Rojas */ @Path("model.item") //@com.sun.jersey.spi.resource.Singleton //@com.sun.jersey.api.spring.Autowire public class ItemRESTFacade { @PersistenceContext(unitName = "CatalogPU") //@Error("Please fix your project manually, for instructions see http://wiki.netbeans.org/SpringWithAopalliance") protected EntityManager entityManager; public ItemRESTFacade() { } @POST @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) @Transactional public Response create(Item entity) { entityManager.persist(entity); return Response.created(URI.create(entity.getItemid().toString())).build(); } @PUT @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) @Transactional public void edit(Item entity) { entityManager.merge(entity); } @DELETE @Path("{id}") @Transactional public void remove(@PathParam("id") String id) { Item entity = entityManager.getReference(Item.class, id); entityManager.remove(entity); } @GET @Path("{id}") @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) @Transactional public Item find(@PathParam("id") String id) { return entityManager.find(Item.class, id); } @GET @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) @Transactional public List<Item> findAll() { return find(true, -1, -1); } @GET @Path("{max}/{first}") @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) @Transactional public List<Item> findRange(@PathParam("max") Integer max, @PathParam("first") Integer first) { return find(false, max, first); } @GET @Path("count") @Produces(MediaType.TEXT_PLAIN) @Transactional public String count() { try { Query query = entityManager.createQuery("SELECT count(o) FROM Item AS o"); return query.getSingleResult().toString(); } finally { entityManager.close(); } } private List<Item> find(boolean all, int maxResults, int firstResult) { try { Query query = entityManager.createQuery("SELECT object(o) FROM Item AS o"); if (!all) { query.setMaxResults(maxResults); query.setFirstResult(firstResult); } return query.getResultList(); } finally { entityManager.close(); } } }