# org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path on EntityManager createQuery

**URL:** <https://discourse.hibernate.org/t/org-hibernate-hql-internal-ast-querysyntaxexception-invalid-path-on-entitymanager-createquery/594>\
**Category:** Hibernate ORM\
**Created:** [April 20, 2018, 8:27pm UTC](https://discourse.hibernate.org/t/org-hibernate-hql-internal-ast-querysyntaxexception-invalid-path-on-entitymanager-createquery/594 "2018-04-20T20:27:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pdeluca](https://avatars.discourse-cdn.com/v4/letter/p/5f9b8f/32.png) [@pdeluca](https://discourse.hibernate.org/u/pdeluca)\
**Post date:** [April 20, 2018, 8:27pm UTC](https://discourse.hibernate.org/t/org-hibernate-hql-internal-ast-querysyntaxexception-invalid-path-on-entitymanager-createquery/594/1 "2018-04-20T20:27:04Z")

</div>

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**

```auto
@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**

```auto
@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;
    }
}

```

**How can I solve this and why is it happening?**

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [April 21, 2018, 4:23am UTC](https://discourse.hibernate.org/t/org-hibernate-hql-internal-ast-querysyntaxexception-invalid-path-on-entitymanager-createquery/594/2 "2018-04-21T04:23:24Z")

</div>

Why do you create an extra count query when you can just get that from the size of the Product list you are fetching?

---

<div class="post-metadata">

**Author:** ![p3consulting](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/p3consulting/32/565_2.png) [@p3consulting](https://discourse.hibernate.org/u/p3consulting)\
**Post date:** [April 21, 2018, 6:28am UTC](https://discourse.hibernate.org/t/org-hibernate-hql-internal-ast-querysyntaxexception-invalid-path-on-entitymanager-createquery/594/3 "2018-04-21T06:28:08Z")

</div>

> [@pdeluca](#):
>
> query.where(predicates.toArray(new Predicate{}));  
> queryCount.where(predicates.toArray(new Predicate{}));

If you have 2 aliases in the generated query, it’s because the Query analyser thinks you are using 2 times the same entity, and I don’t see another suspect to create that kind of problem that the fact you reuse the predicates.  
Guessing: when you add predicates using `query.where` it is probably introducing a reference to the root of the query somewhere in the predicate, then when you add the same predicate to another query you “pollute” it with another root entity.

(if my theory is correct, reverse the 2 `query….where()`and the exception should be on the other query…)

---

<div class="post-metadata">

**Author:** ![pdeluca](https://avatars.discourse-cdn.com/v4/letter/p/5f9b8f/32.png) [@pdeluca](https://discourse.hibernate.org/u/pdeluca)\
**Post date:** [April 23, 2018, 1:15pm UTC](https://discourse.hibernate.org/t/org-hibernate-hql-internal-ast-querysyntaxexception-invalid-path-on-entitymanager-createquery/594/4 "2018-04-23T13:15:22Z")

</div>

If I reverse the 2 query…where, the issue persist on the same line. Very weird.

---

<div class="post-metadata">

**Author:** ![pdeluca](https://avatars.discourse-cdn.com/v4/letter/p/5f9b8f/32.png) [@pdeluca](https://discourse.hibernate.org/u/pdeluca)\
**Post date:** [April 23, 2018, 1:16pm UTC](https://discourse.hibernate.org/t/org-hibernate-hql-internal-ast-querysyntaxexception-invalid-path-on-entitymanager-createquery/594/5 "2018-04-23T13:16:49Z")

</div>

But I am fetching only the 10 first results (or 25 or 100 if I selected it in the front end).  
And what I need if to count all the records in my DB with that conditions.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [April 23, 2018, 1:25pm UTC](https://discourse.hibernate.org/t/org-hibernate-hql-internal-ast-querysyntaxexception-invalid-path-on-entitymanager-createquery/594/6 "2018-04-23T13:25:25Z")

</div>

Try to replicate it with this [test case](http://in.relation.to/2016/01/14/hibernate-jpa-test-case-template/) first which wwe can take a look on.

---

<div class="post-metadata">

**Author:** ![p3consulting](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/p3consulting/32/565_2.png) [@p3consulting](https://discourse.hibernate.org/u/p3consulting)\
**Post date:** [April 23, 2018, 1:28pm UTC](https://discourse.hibernate.org/t/org-hibernate-hql-internal-ast-querysyntaxexception-invalid-path-on-entitymanager-createquery/594/7 "2018-04-23T13:28:55Z")

</div>

Well, try to NOT share the predicates between the 2 queries… and this means creating them twice.

---

<div class="post-metadata">

**Author:** ![pdeluca](https://avatars.discourse-cdn.com/v4/letter/p/5f9b8f/32.png) [@pdeluca](https://discourse.hibernate.org/u/pdeluca)\
**Post date:** [April 23, 2018, 3:05pm UTC](https://discourse.hibernate.org/t/org-hibernate-hql-internal-ast-querysyntaxexception-invalid-path-on-entitymanager-createquery/594/8 "2018-04-23T15:05:40Z")

</div>

@vlad, @p3consulting

Finally solved refactoring:

```auto
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.
    //...
}

```
