Hibernate restriction + projection org.hibernate.QueryException:无法解析属性

时间:2014-11-29 03:05:14

标签: java hibernate

当对投影中的字段应用限制时,我有以下例外:

  

org.hibernate.QueryException:无法解析属性:surcursal:com.adm_colas.model.AnalisisColas       在org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83)       在org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:77)       在org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1978)       at org.hibernate.loader.criteria.EntityCriteriaInfoProvider.getType(EntityCriteriaInfoProvider.java:57)       at org.hibernate.loader.criteria.CriteriaQueryTranslator.getPathInfo(CriteriaQueryTranslator.java:245)       在org.hibernate.loader.criteria.CriteriaQueryTranslator.createCriteriaEntityNameMap(CriteriaQueryTranslator.java:229)       在org.hibernate.loader.criteria.CriteriaQueryTranslator。(CriteriaQueryTranslator.java:112)       在org.hibernate.loader.criteria.CriteriaLoader。(CriteriaLoader.java:88)       在org.hibernate.internal.SessionImpl.list(SessionImpl.java:1663)       在org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:380)       在org.hibernate.internal.CriteriaImpl.uniqueResult(CriteriaImpl.java:402)       在com.adm_colas.dao.AnalisisColasDAOImpl.getSumGroupBy(AnalisisColasDAOImpl.java:121)

这是我要查询的课程:

 @Entity
 @Table(name = "analisis_colas", catalog = "colas")
 public class AnalisisColas implements java.io.Serializable {
/**
 * 
 */
private static final long serialVersionUID = 1L;

private Integer id;
private Date fecha;
private Sucursales sucursal;
private String dia;
private Double semana;
private Integer mes;
private String turno;
private Turnos turnoSucursal;
private Timestamp horaDesde;
private Double tasaEntrada;
private Double clientesNoAtendidos;
private Double tasaServicio;
private Date tiempoEsperaPromedio;
private Timestamp tiempoServicioPromedio;
private Timestamp tiempoTotalPromedio;
private Double tiempoTotalPromedioDec;
private Timestamp maximoTiempoEspera;
private Timestamp maximoTiempoServicio;
private Timestamp horaHasta;
private Double utilizacion;
private Double probSistemaVacio;
private Double probabilidadClienteEsper;
private Double nivelServicioActual;

这是破解我头脑的代码:

 public List<AnalisisColas> getGroupBy(AnalisisColas a) {
    Session session = this.sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(AnalisisColas.class);

    criteria.setProjection(
            Projections
                    .projectionList()
                    .add(Projections.groupProperty("sucursal"), "sucursal")
                    .add(Projections.groupProperty("dia"), "dia")
                    .add(Projections.groupProperty("semana"), "semana")
                    .add(Projections.groupProperty("mes"), "mes")
                    .add(Projections.groupProperty("turnoSucursal"),
                            "turnoSucursal")
                    .add(Projections.avg("tasaEntrada"), "tasaEntrada")
                    .add(Projections.avg("tasaServicio"), "tasaServicio"))
            .setResultTransformer(
                    Transformers.aliasToBean(AnalisisColas.class));

    if (a.getSucursal() != null) {
        criteria.createAlias("surcursal", "s").add(
                Restrictions.like("s.nombre", a.getSucursal().getNombre()));
    }
    if (a.getMes() != null) {
        criteria.add(Restrictions.eq("mes", a.getMes()));
    }
    if (a.getSemana() != null) {
        criteria.add(Restrictions.eq("semana", a.getSemana()));
    }
    if (a.getDia() != null) {
        criteria.add(Restrictions.eq("dia", a.getDia()));
    }
    if (a.getTurnoSucursal() != null) {
        criteria.createAlias("turnoSucursal", "ts").add(
                Restrictions.eq("ts.turnoId", a.getTurnoSucursal()
                        .getTurnoId()));
    }
    List<AnalisisColas> analisisColasList = criteria.list();
    return analisisColasList;
}

任何想法都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

不确定西班牙语名称,即关于含义......但Hibernate信息非常明确:

  

无法解析属性: surcursal :com.adm_colas.model.AnalisisColas

请参阅 surcursal 一词,主要是 sur ...

您的实体是

public class AnalisisColas implements java.io.Serializable {
...
private Sucursales sucursal;

名称是: sucursal ,主要是开头 suc ......

所以,这部分查询是错误的:

if (a.getSucursal() != null) {    
    // here SUR... instead of SUC...
    criteria.createAlias("surcursal", "s").add( 
            Restrictions.like("s.nombre", a.getSucursal().getNombre()));