简体   繁体   中英

QuerySyntaxException hibernate invalid path on EntityManager createQuery

I am sending from the frontend a value to search on two properties of my entity Producto . That properties are Codigo and Descripcion .

The issue is that when the line TypedQuery<Long> typedQuery = em.createQuery(queryCount); hits, this exception is thrown:

queryString= org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'generatedAlias1._codigo' [select count(generatedAlias0) from com.its.entidades.db.Producto as generatedAlias0 where ( generatedAlias1._codigo like :param0 ) and ( generatedAlias1._descripcion like :param1 )]


detailMessage= Invalid path: 'generatedAlias1._codigo'

The weird thing is that if I comment the quoted line, and in consequence the two lines below, everything runs as expected. But I need to get the total of the registers filtered, so I need to count them.

ProductoService.java

@Override
public ServiceResponse<List<Producto>> ObtenerListaPaginada(ParametrosListadoModelo parametros) {

    ServiceResponse<List<Producto>> ret = new ServiceResponse<>();
    ret.setListadoModelo(parametros);

    try {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Producto> query = cb.createQuery(Producto.class);

        CriteriaQuery<Long> queryCount = cb.createQuery(Long.class);
        queryCount.select(cb.count(queryCount.from(Producto.class)));

        Root<Producto> entity = query.from(Producto.class);
        TypedQuery<Producto> tq = null;

        if (parametros.getBusqueda() != null && !parametros.getBusqueda().isEmpty()) {
            String queryFilter = "%" + parametros.getBusqueda() + "%";

            List<Predicate> predicates = new ArrayList<>();
            predicates.add(cb.like(entity.<String>get("_codigo"), queryFilter));
            predicates.add(cb.like(entity.<String>get("_descripcion"), queryFilter));

            query.where(predicates.toArray(new Predicate[]{}));
            queryCount.where(predicates.toArray(new Predicate[]{}));
        }

        // Count for total
        TypedQuery<Long> typedQuery = em.createQuery(queryCount);
        Long count = typedQuery.getSingleResult();
        ret.getListadoModelo().setTotalRegistros(count);

        // Order by
        if (parametros.getCampoOrdenamiento().equals("codigo"))
            parametros.setCampoOrdenamiento("_codigo");
        if (parametros.getCampoOrdenamiento().equals("descripcion"))
            parametros.setCampoOrdenamiento("_descripcion");

        query.orderBy(parametros.getDireccionOrdenamiento().equals("ASC") ? cb.asc(entity.get(parametros.getCampoOrdenamiento())) : cb.desc(entity.get(parametros.getCampoOrdenamiento())));

        // Paginator
        tq = em.createQuery(query);
        tq.setFirstResult((int) ((parametros.getNumeroPagina() - 1) * parametros.getCantidadElementos()));
        tq.setMaxResults((int) (parametros.getCantidadElementos()));
        ret.setData(tq.getResultList());
    } catch (Exception ex) {
        ret.getErrores().add(new ServicioError(ex));
    }

    return ret;
}

Producto.java

@Entity(name = "Producto")
public class Producto {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "ProductoID")
    private int _productoID;

    @Column(name = "cCodigo")
    private String _codigo;

    @Column(name="cDescripcion")
    private String _descripcion;

    @JsonProperty("codigo")
    public String getCodigo() {
        return _codigo;
    }

    @JsonProperty("codigo")
    public void setCodigo(String _codigo) {
        this._codigo = _codigo;
    }

    @JsonProperty("descripcion")
    public String getDescripcion() {
        return _descripcion;
    }

    @JsonProperty("descripcion")
    public void setDescripcion(String _descripcion) {
        this._descripcion = _descripcion;
    }
}

Why is this happening?

Finally solved refactoring:

try{
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Producto> query = cb.createQuery(Producto.class);

    CriteriaQuery<Long> queryCount = cb.createQuery(Long.class);
    Root<Producto> entityRoot = queryCount.from(query.getResultType());
    queryCount.select(cb.count(entityRoot));

    Root<Producto> entity = query.from(Producto.class);
    TypedQuery<Producto> tq;
    //And the rest of the code is the same as the original one.
    //...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM