title: Restful java Web service from database in netbeans
body: <p>Sorry for my English, is not very good.</p>
  
  <p>I created a Restful webservice with NetBeans (restful from database). 
  This creates the entity classes and the facade (with its path, get etc) </p>
  
  <p>UsersFacadeRest.java</p>
  
  <pre><code>@Stateless
  @Path("glee.users")
  public class UsersFacadeREST extends AbstractFacade&lt;Users&gt; {
  @PersistenceContext(unitName = "WebApplication6PU")
  private EntityManager em;
  
  public UsersFacadeREST() {
      super(Users.class);
  }
  
  @POST
  @Override
  @Consumes({"application/xml", "application/json"})
  public void create(Users entity) {
      super.create(entity);
  }
  @PUT
  @Path("{id}")
  @Consumes({"application/xml", "application/json"})
  public void edit(@PathParam("id") Integer id, Users entity) {
      super.edit(entity);
  }
  @DELETE
  @Path("{id}")
  public void remove(@PathParam("id") Integer id) {
      super.remove(super.find(id));
  }
  
  @GET
  @Path("{id}")
  @Produces({"application/xml", "application/json"})
  public Users find(@PathParam("id") Integer id) {
      return super.find(id);
  }
  @GET
  @Override
  @Produces({"application/xml", "application/json"})
  public List&lt;Users&gt; findAll() {
      return super.findAll();
  }
  @GET
  @Path("{from}/{to}")
  @Produces({"application/xml", "application/json"})
  public List&lt;Users&gt; findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
      return super.findRange(new int[]{from, to});
  }
  
  @GET
  @Path("count")
  @Produces("text/plain")
  public String countREST() {
      return String.valueOf(super.count());
  }
  @Override
  protected EntityManager getEntityManager() {
      return em;
  }
  }
  </code></pre>
  
  <p>What changes would have to do so that instead of filtering by id, putting data could reimburse the year? I explained. 
  With this code I write in the browser: </p>
  
  <pre><code> http://localhost:8080/WebApplication6/webresources/glee.users/3 
  </code></pre>
  
  <p>And it returns an xml data with id 3. 
  Ok, basically I want to change my code to: </p>
  
  <pre><code> http://localhost:8080/WebApplication6/webresources/glee.users/2011 (filter by year     
   2011)
  </code></pre>
  
  <p>and this returns me all users with 2011.</p>
  
  <p>Users.java</p>
  
  <pre><code>@Entity
  @Table(name = "users")
  @XmlRootElement
  @NamedQueries({
  @NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
  @NamedQuery(name = "Users.findById", query = "SELECT u FROM Users u WHERE u.id = :id"),
  @NamedQuery(name = "Users.findByName", query = "SELECT u FROM Users u WHERE u.name =   
  :name"),
  @NamedQuery(name = "Users.findByTelephone", query = "SELECT u FROM Users u WHERE 
  u.telephone = :telephone"),
  @NamedQuery(name = "Users.findByYear", query = "SELECT u FROM Users u WHERE u.year = 
  :year")})
  public class Users implements Serializable {
  private static final long serialVersionUID = 1L;
  @Id
  @Basic(optional = false)
  @NotNull
  @Column(name = "id")
  private Integer id;
  @Size(max = 25)
  @Column(name = "name")
  private String name;
  @Column(name = "telephone")
  private Integer telephone;
  @Column(name = "year")
  private Integer year;
  
  public Users() {
  }
  
  public Users(Integer id) {
      this.id = id;
  }
  
  public Integer getId() {
      return id;
  }
  
  public void setId(Integer id) {
      this.id = id;
  }
  
  public String getName() {
      return name;
  }
  
  public void setName(String name) {
      this.name = name;
  }
  
  public Integer getTelephone() {
      return telephone;
  }
  
  public void setTelephone(Integer telephone) {
      this.telephone = telephone;
  }
  
  public Integer getYear() {
      return year;
  }
  
  public void setYear(Integer year) {
      this.year = year;
  }
  
  @Override
  public int hashCode() {
      int hash = 0;
      hash += (id != null ? id.hashCode() : 0);
      return hash;
  }
  
  @Override
  public boolean equals(Object object) {
      // TODO: Warning - this method won't work in the case the id fields are not set
      if (!(object instanceof Users)) {
          return false;
      }
      Users other = (Users) object;
      if ((this.id == null &amp;&amp; other.id != null) || (this.id != null &amp;&amp; !this.id.equals(other.id))) {
          return false;
      }
      return true;
  }
  
  @Override
  public String toString() {
      return "glee.Users[ id=" + id + " ]";
  }
  
  }
  </code></pre>
  
  <p>THANKS !</p>
  
  <p>abstractFacade</p>
  
  <pre><code>public abstract class AbstractFacade&lt;T&gt; {
      private Class&lt;T&gt; entityClass;
  
      public AbstractFacade(Class&lt;T&gt; entityClass) {
          this.entityClass = entityClass;
      }
  
      protected abstract EntityManager getEntityManager();
  
      public void create(T entity) {
          getEntityManager().persist(entity);
      }
  
      public void edit(T entity) {
          getEntityManager().merge(entity);
      }
  
      public void remove(T entity) {
          getEntityManager().remove(getEntityManager().merge(entity));
      }
  
      public T find(Object id) {
          return getEntityManager().find(entityClass, id);
      }
  
      public List&lt;T&gt; findAll() {
          javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
          cq.select(cq.from(entityClass));
          return getEntityManager().createQuery(cq).getResultList();
      }
  
      public List&lt;T&gt; findRange(int[] range) {
          javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
          cq.select(cq.from(entityClass));
          javax.persistence.Query q = getEntityManager().createQuery(cq);
          q.setMaxResults(range[1] - range[0] + 1);
          q.setFirstResult(range[0]);
          return q.getResultList();
      }
  
      public int count() {
          javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
          javax.persistence.criteria.Root&lt;T&gt; rt = cq.from(entityClass);
          cq.select(getEntityManager().getCriteriaBuilder().count(rt));
          javax.persistence.Query q = getEntityManager().createQuery(cq);
          return ((Long) q.getSingleResult()).intValue();
      }
  
  }
  </code></pre>