Generate Query in Web Service Java

0

I have a Restful web service in Java, I have been able to do CRUD transactions without any problem, but the challenge I have encountered is how to do inner join and filters in a query. I have something like this:

@GET
@Path("buscar")
@Produces(MediaType.APPLICATION_JSON)

public List<Categorias> buscar(String descripcion) {
    javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass)); //Tengo entendido que aquí se hace pero desconozco el cómo. 
    javax.persistence.Query q = getEntityManager().createQuery(cq);
    return null;//Lo dejo null para armar la Query primero
}

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

Somebody could guide me in this please ...

    
asked by Jonathan 19.04.2018 в 02:46
source

1 answer

2

First of all, my recommendation is that you do not do the database accesses directly in the Controller, it uses a DAO layer to separate the layers (good practices). By using entities you can use JPQL, which facilitates queries:

String sql = "SELECT e FROM Entidad e INNER JOIN e.EntidadRelacionada er WHERE er.id = :id";
Query query = getEntityManager().createQuery(sql);
query.setParameter("id", 1);
Entidad entidad = query.getSingleResult();
    
answered by 19.04.2018 / 08:49
source