gpt4 book ai didi

java - EntityManager createQuery 上的 QuerySyntaxException hibernate 无效路径

转载 作者:行者123 更新时间:2023-12-02 11:20:39 26 4
gpt4 key购买 nike

我从前端发送一个值来搜索我的实体 Producto 的两个属性。该属性是CodigoDescription

问题是当 TypedQuery<Long> typedQuery = em.createQuery(queryCount); 行时点击,抛出此异常:

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'

奇怪的是,如果我评论引用的行,以及下面的两行,一切都会按预期运行。但我需要过滤寄存器的总数,因此我需要对它们进行计数。

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;
}
}

为什么会发生这种情况?

最佳答案

最终解决了重构:

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

关于java - EntityManager createQuery 上的 QuerySyntaxException hibernate 无效路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49948519/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com