当前位置: 首页 > 知识库问答 >
问题:

Hql查询到条件查询

南宫云
2023-03-14
"SELECT gmr.id.expedientId.idEns as idEns,"
            + " gmr.id.expedientId.anyExp as anyExp,"
            + " gmr.id.expedientId.numExp as numExp,"
            + " gmr.id.numOrdre as numOrdre,"
            + " gmr.idRecursRebuig.desRecursRebuig as descripcioRebuig,"
            + " gmr.desAmpliRebuig as observacionsRebuig,"
            + " gmr.tipusRebuig as tipusRebuig"
            + " FROM GirMotiuRebuig gmr"
            + " where gmr.id.expedientId.idEns =:idEns and"
            + " gmr.id.expedientId.anyExp =:anyExp and"
            + " gmr.id.expedientId.numExp =:numExp"
            + " ORDER BY gmr.id.numOrdre";

            Query queryObject = getSessionFactory().getCurrentSession()
                    .createQuery(queryString);

            queryObject.setResultTransformer(Transformers
                    .aliasToBean(MotiusRebuig.class));

            queryObject.setParameter("idEns",clauExpedient.getIdEns());
            queryObject.setParameter("anyExp",clauExpedient.getAnyExp());
            queryObject.setParameter("numExp",clauExpedient.getNumExp());

            llistaMotiusRebuig =queryObject.list();
Criteria consulta = this.getSessionFactory().getCurrentSession()
    .createCriteria(GirMotiuRebuig.class)
    .add(Restrictions.eq("id.expedientId.idEns", clauExpedient.getIdEns()))
    .add(Restrictions.eq("id.expedientId.anyExp", clauExpedient.getAnyExp()))
    .add(Restrictions.eq("id.expedientId.numExp", clauExpedient.getNumExp()))
    .addOrder(Order.asc("id.numOrdre"))
    .setProjection(Projections.projectionList()
    .add(Projections.property("id.expedientId.idEns"),"idEns")
    .add(Projections.property("id.expedientId.anyExp"),"anyExp")
    .add(Projections.property("id.expedientId.numExp"),"numExp")
    .add(Projections.property("id.numOrdre"),"numOrdre")
        .add(Projections.property("idRecursRebuig.desRecursRebuig"),"descripcioRebuig")
    .add(Projections.property("desAmpliRebuig"),"observacionsRebuig")
    .add(Projections.property("tipusRebuig"),"tipusRebuig")
    .add(Projections.property("expedientId.numOrdre"),"numOrdre"))
                .setResultTransformer(
                Transformers.aliasToBean(MotiusRebuig.class));
    llistaMotiusRebuig =  consulta.list();

和异常:

Caused by: org.hibernate.QueryException: could not resolve property: idRecursRebuig.desRecursRebuig of: cat.base.gir.logica.domini.impl.GirMotiuRebuig
        at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:44)
        at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:38)
        at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1362)
        at org.hibernate.loader.criteria.CriteriaQueryTranslator.getType(CriteriaQueryTranslator.java:477)
        at org.hibernate.criterion.PropertyProjection.getTypes(PropertyProjection.java:36)
        at org.hibernate.criterion.AliasedProjection.getTypes(AliasedProjection.java:37)
        at org.hibernate.criterion.ProjectionList.getTypes(ProjectionList.java:38)
        at org.hibernate.loader.criteria.CriteriaQueryTranslator.getProjectedTypes(CriteriaQueryTranslator.java:318)
        at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:77)
        at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:59)
        at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:67)
        at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1550)
        at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
        at cat.base.gir.dao.GirMotiuRebuigDao.obtenirMotiusRebuig(GirMotiuRebuigDao.java:73)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
        at $Proxy1183.obtenirMotiusRebuig(Unknown Source)
        at cat.base.gir.serveis.GirRecursService.obtenirMotiusRebuig(GirRecursService.java:381)
        ... 127 more

实体:GirMotiuRebuig

@Entity
@Table(name = "gir_exp_motiu_rebuig")
public class GirMotiuRebuig implements IGirMotiuRebuig {

    /**
     * 
     */
    private static final long serialVersionUID = -5587115692447683913L;

    private ExpedientIdMultiple id;
    private MRecursRebuig idRecursRebuig;
    private String desAmpliRebuig;
    private GirExpedient expedientGir;
    protected Integer tipusRebuig;

    public GirMotiuRebuig() {
    }

    public GirMotiuRebuig(ExpedientIdMultiple id, MRecursRebuig idRecursRebuig, String desAmpliRebuig,
            GirExpedient expedientGir) {
        this.id = id;
        this.idRecursRebuig = idRecursRebuig;
        this.desAmpliRebuig = desAmpliRebuig;
        this.expedientGir = expedientGir;
    }

    @EmbeddedId
    @AttributeOverrides( {
            @AttributeOverride(name = "expedientId.idEns", column = @Column(name = "id_ens", nullable = false, precision = 5, scale = 0)),
            @AttributeOverride(name = "expedientId.anyExp", column = @Column(name = "any_exp", nullable = false, precision = 4, scale = 0)),
            @AttributeOverride(name = "expedientId.numExp", column = @Column(name = "num_exp", nullable = false, precision = 10, scale = 0)),
            @AttributeOverride(name = "numOrdre", column = @Column(name = "num_ordre", nullable = false, precision = 10, scale = 0)) })
    public ExpedientIdMultiple getId() {
        return id;
    }

    public void setId(ExpedientIdMultiple id) {
        this.id = id;
    }

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_recurs_rebuig")
    public MRecursRebuig getIdRecursRebuig() {
        return idRecursRebuig;
    }

    public void setIdRecursRebuig(MRecursRebuig idRecursRebuig) {
        this.idRecursRebuig = idRecursRebuig;
    }

    @Column(name = "des_ampli_rebuig", nullable = false, length = 250)
    public String getDesAmpliRebuig() {
        return desAmpliRebuig;
    }

    public void setDesAmpliRebuig(String desAmpliRebuig) {
        this.desAmpliRebuig = desAmpliRebuig;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumns( {
            @JoinColumn(name = "id_ens", referencedColumnName = "id_ens", nullable = false, insertable = false, updatable = false),
            @JoinColumn(name = "any_exp", referencedColumnName = "any_exp", nullable = false, insertable = false, updatable = false),
            @JoinColumn(name = "num_exp", referencedColumnName = "num_exp", nullable = false, insertable = false, updatable = false) })
    public GirExpedient getExpedientGir() {
        return expedientGir;
    }

    public void setExpedientGir(GirExpedient expedientGir) {
        this.expedientGir = expedientGir;
    }

    @Column(name = "tipus_rebuig", nullable = false)
    public Integer getTipusRebuig() {
        return tipusRebuig;
    }

    public void setTipusRebuig(Integer tipusRebuig) {
        this.tipusRebuig = tipusRebuig;
    }

这个标准起作用了:

Criteria consulta = this.getSessionFactory().getCurrentSession()
                .createCriteria(GirMotiuRebuig.class, "g")
                    .add(Restrictions.eq("g.id.expedientId.idEns", clauExpedient.getIdEns()))
                    .add(Restrictions.eq("g.id.expedientId.anyExp", clauExpedient.getAnyExp()))
                    .add(Restrictions.eq("g.id.expedientId.numExp", clauExpedient.getNumExp()))
                    .addOrder(Order.asc("g.id.numOrdre"))
                .setProjection(Projections.projectionList()
                        .add(Projections.property("g.id.expedientId.idEns"),"idEns")
                        .add(Projections.property("g.id.expedientId.anyExp"),"anyExp")
                        .add(Projections.property("g.id.expedientId.numExp"),"numExp")
                        .add(Projections.property("g.desAmpliRebuig"),"observacionsRebuig")
                        .add(Projections.property("g.id.numOrdre"),"numOrdre")
                        .add(Projections.property("g.tipusRebuig"),"tipusRebuig")
                        )
                .setResultTransformer(
                        Transformers.aliasToBean(MotiusRebuig.class));
        llistaMotiusRebuig =  consulta.list();
Criteria consulta = this.getSessionFactory().getCurrentSession()
                    .createCriteria(GirMotiuRebuig.class, "g")
                        .add(Restrictions.eq("g.id.expedientId.idEns", clauExpedient.getIdEns()))
                        .add(Restrictions.eq("g.id.expedientId.anyExp", clauExpedient.getAnyExp()))
                        .add(Restrictions.eq("g.id.expedientId.numExp", clauExpedient.getNumExp()))
                        .addOrder(Order.asc("g.id.numOrdre"))
                    .setProjection(Projections.projectionList()
                            .add(Projections.property("g.id.expedientId.idEns"),"idEns")
                            .add(Projections.property("g.id.expedientId.anyExp"),"anyExp")
                            .add(Projections.property("g.id.expedientId.numExp"),"numExp")
                            .add(Projections.property("g.desAmpliRebuig"),"observacionsRebuig")
                            .add(Projections.property("g.id.numOrdre"),"numOrdre")
                            .add(Projections.property("g.tipusRebuig"),"tipusRebuig")
                            **.add(Projections.property("g.idRecursRebuig.desRecursRebuig"),"descripcioRebuig")**
                            )
                    .setResultTransformer(
                            Transformers.aliasToBean(MotiusRebuig.class));
            llistaMotiusRebuig =  consulta.list();
nested exception is org.hibernate.QueryException: could not resolve property: idRecursRebuig.desRecursRebuig of: cat.base.gir.logica.domini.impl.GirMotiuRebuig
@Entity
@Table(name = "MRecursRebuig")
public class MRecursRebuig implements IMRecursRebuig {

    /**
     * 
     */
    private static final long serialVersionUID = 7439723582297629986L;

    private Integer idRecursRebuig;
    private String desRecursRebuig;
    private Integer tipusRebuig;

    public MRecursRebuig() {
    }

    public MRecursRebuig(Integer idRecursRebuig, String desRecursRebuig) {
        this.idRecursRebuig = idRecursRebuig;
        this.desRecursRebuig = desRecursRebuig;
    }

    @Id
    @Column(name = "id_recurs_rebuig", unique = true, nullable = false, precision = 22, scale = 0)
    public Integer getIdRecursRebuig() {
        return idRecursRebuig;
    }

    public void setIdRecursRebuig(Integer idRecursRebuig) {
        this.idRecursRebuig = idRecursRebuig;
    }

    @Column(name = "des_recurs_rebuig", nullable = false, length = 100)
    public String getDesRecursRebuig() {
        return desRecursRebuig;
    }

    public void setDesRecursRebuig(String desRecursRebuig) {
        this.desRecursRebuig = desRecursRebuig;
    }

    @Column(name = "tipus_rebuig", nullable = false)
    public Integer getTipusRebuig() {
        return tipusRebuig;
    }
Criteria consulta = this.getSessionFactory().getCurrentSession()
                    .createCriteria(GirMotiuRebuig.class, "g")
                    .createAlias("g.idRecursRebuig", "idRecursRebuig")
                        .add(Restrictions.eq("g.id.expedientId.idEns", clauExpedient.getIdEns()))
                        .add(Restrictions.eq("g.id.expedientId.anyExp", clauExpedient.getAnyExp()))
                        .add(Restrictions.eq("g.id.expedientId.numExp", clauExpedient.getNumExp()))
                        .add(Restrictions.eq("g.idRecursRebuig", clauExpedient.getNumExp()))
                        .addOrder(Order.asc("g.id.numOrdre"))
                    .setProjection(Projections.projectionList()
                            .add(Projections.property("g.id.expedientId.idEns"),"idEns")
                            .add(Projections.property("g.id.expedientId.anyExp"),"anyExp")
                            .add(Projections.property("g.id.expedientId.numExp"),"numExp")
                            .add(Projections.property("g.desAmpliRebuig"),"observacionsRebuig")
                            .add(Projections.property("g.id.numOrdre"),"numOrdre")
                            .add(Projections.property("g.tipusRebuig"),"tipusRebuig")
                            **.add(Projections.property("idRecursRebuig.desRecursRebuig"),"descripcioRebuig")**
                            )
                    .setResultTransformer(
                            Transformers.aliasToBean(MotiusRebuig.class));
            llistaMotiusRebuig =  consulta.list();
nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of cat.base.gir.logica.domini.impl.MRecursRebuig.idRecursRebuig
    cat.base.gir.serveis.GirRecursService.obtenirMotiusRebuig(GirRecursService.java:387)

共有1个答案

于捷
2023-03-14

在HQL查询中,当您有类似

select entityA.entityB.foo from EntityA entityA ...

实际上,您在EntityA和EntityB之间创建了一个隐式内部连接。相当于

select entityB.foo from EntityA entityA
inner join entityA.entityB entityB ...

在条件查询中,不能像在HQL中那样对属性进行连锁。相反,每次需要连接时,您必须使用子条件或(我更喜欢)别名来明确地创建它。所以你需要做

Criteria consulta = this.getSessionFactory().getCurrentSession()
    .createCriteria(GirMotiuRebuig.class, "girMotiuRebuig")
    .createAlias("girMotiuRebuig.id.expedientId", "expedient") 
    .add(Restrictions.eq("expedient.idEns", clauExpedient.getIdEns()))
    ...
 类似资料:
  • 我应该如何将这个MySQL查询转换为HQL来检索我需要的一些数据? 在我的代码中,有POJO ProcessedUrl,它映射了id/url/date/set(句子)字段。我想做一些类似的事情: 然后遍历此结果,为每个URL打印&。 我知道我的查询应该返回中的字段,那么我如何返回一些分组查询并从中检索不直接保存在表中的数据呢?就像每个URL的一样? 多谢帮忙。

  • 问题内容: 我有这个查询,我需要在hql中将其转换,但我有点困惑,不知道如何编写hql中“ where”子句中出现的条件。 什么是hql等效于此sql查询? 问题答案: 请在HQL查询下面找到:假设p_message和p_config是域对象名称,其他是映射到列的属性。

  • 我对冬眠很陌生。 所有实体都与表名和列名相同。这个查询在mysql中运行良好。我想用HQL编写这个查询。 我将如何在hql中编写这个?请任何人帮帮我。

  • v3.0开始,herosphp 提供了一套全新的设置查询条件的接口: MysqlModel::where($field, $opt, $value); 参数名称 参数类型 参数说明 $field string OR function 字段名称, 如果传入的是 function 的话,则说明是闭包. 闭包传入的是一组查询条件。 $opt string 操作符,如果不传入 $value 的情况下,$o

  • 本文向大家介绍php查询及多条件查询,包括了php查询及多条件查询的使用技巧和注意事项,需要的朋友参考一下 单条件查询: 1.先要有一张表,显示出表中的数据: 上图: 啥都没改的一张表 2.再来个from表单,让用户输入,点击查询: 如图: 3.建立关键字查询: 图: 多条件查询: 前面照旧; 出了php的语句: 效果图: 这样:有几个条件就做几个条件变量,第一个条件不为空就执行的第一个条件,第二

  • 主要内容:单一条件的查询语句,多条件的查询语句在 MySQL 中,如果需要有条件的从数据表中查询数据,可以使用 WHERE 关键字来指定查询条件。 使用 WHERE 关键字的语法格式如下: WHERE 查询条件 查询条件可以是: 带比较运算符和逻辑运算符的查询条件 带 BETWEEN AND 关键字的查询条件 带 IS NULL 关键字的查询条件 带 IN 关键字的查询条件 带 LIKE 关键字的查询条件 单一条件的查询语句 单一条件指的是在