Search for registration in Web Service Java

1

I am starting with this from the Web Service Rest and I have encountered the problem when doing a crud with the database.

For example, I need to search for a record but not for its PK but for another field, is it possible to do this? Could you guide me on how to do it? I searched but everything is very confusing

Help please!

This is one of the classes mapped from the Oracle DB, I'm not using frameworks, I only create them with RestFull Web Services from Database.

In this class, I need to filter by mail and password ...

package entidad.servicio;

import entidad.Usuario;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
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;

@Stateless
@Path("entidad.usuario")
public class UsuarioFacadeREST extends AbstractFacade<Usuario> {

@PersistenceContext(unitName = "WSPU")
private EntityManager em;

public UsuarioFacadeREST() {
    super(Usuario.class);
}

@POST
@Override
@Consumes(MediaType.APPLICATION_JSON)
public void create(Usuario entity) {
    super.create(entity);
}

@PUT
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)
public void edit(@PathParam("id") Short id, Usuario entity) {
    super.edit(entity);
}

@DELETE
@Path("{id}")
public void remove(@PathParam("id") Short id) {
    super.remove(super.find(id));
}

@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Usuario find(@PathParam("id") Short id) {
    return super.find(id);
}

@GET
@Override
@Produces(MediaType.APPLICATION_JSON)
public List<Usuario> findAll() {
    return super.findAll();
}

@GET
@Path("{from}/{to}")
@Produces(MediaType.APPLICATION_JSON)
public List<Usuario> findRange(@PathParam("from") Integer from,   
@PathParam("to") Integer to) {
    return super.findRange(new int[]{from, to});
}
@GET
@Path("count")
@Produces(MediaType.TEXT_PLAIN)
public String countREST() {
    return String.valueOf(super.count());
}

@Override
protected EntityManager getEntityManager() {
    return em;
   }

}
    
asked by Jonathan 27.04.2018 в 04:43
source

1 answer

1

The abstract class AbstractFacade creates you the basics to access the entity in your database. If you want some other more specific query you have to create it yourself. Assuming you have your class Usuario annotated with @Entity and added to persistence.xml , you could do the following:

private List<Usuario> findUsuarioByField(EntityManager em, String value) {
    return em.createQuery("SELECT u FROM Usuario u where u.field = :value")
        .setParameter("value", value).getResultList();
}

I've put field , both in the name of the method and in the query (JPQL) because I do not know which field you're going to filter through.

My recommendation: take all your queries to class Usuario . That way you'll have everything centralized in one place. AbstractFacade does not give you too much to use (in my opinion). If you finally decide to carry out my recommendation, you just have to take the previous method to the class Usuario and put it as public static instead of private and you only have to invoke it like this: Usuario.findUsuarioByField

    
answered by 28.04.2018 / 12:07
source