简体   繁体   中英

Hibernate forming the wrong query it seems

My requirement is to extract all the distinct rows(name column alone) where name is not null This is my hibernate code.

DetachedCriteria criteria = DetachedCriteria.forClass(A.class);
criteria.add(Restrictions.isNotNull("name"));
ProjectionList list = Projections.projectionList();
list.add(Projections.distinct(Projections.property("name")),"name");
criteria.setProjection(list);
criteria.setResultTransformer(Transformers.aliasToBean(A.class));
result = getHibernateTemplate().findByCriteria(criteria);

The SQL formed is below:

select distinct this_.name as y0_ 
from dbo.A this_ 
where y0_ is not null 

The error is ERROR - Invalid column name 'y0_' I dont understand why hibernate is forming the wrong query like this. Any help appreciated.

Code for class A:

@Entity
@Table(name = "A", uniqueConstraints = {})
public class A implements java.io.Serializable {

    private int skillId;
    private String name;

    public A() {
    }

    @Id
    @Column(name = "SKILL_ID", unique = true, nullable = false, insertable = true, updatable = true)
    @GeneratedValue(strategy = GenerationType.AUTO)
    public int getSkillId() {
        return this.skillId;
    }

    public void setSkillId(int skillId) {
        this.skillId = skillId;
    }

    }

    @Column(name = "name", unique = false, nullable = true, insertable = true, updatable = true, length = 100)
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Try this

DetachedCriteria criteria = DetachedCriteria.forClass(A.class);
criteria.add(Restrictions.isNotNull("name"));
ProjectionList list = Projections.projectionList();
list.add(Projections.property("name"));
criteria.setProjection(Projections.distinct(list));

Try out following:

Criteria criteria = session.createCriteria(Test.class);
ProjectionList projectionList = Projections.projectionList();
ProjectionList projectionList2 = Projections.projectionList();
projectionList2.add(Projections.distinct(projectionList.add(Projections.property("distinctColumn"), "distinctColumn")));
projectionList2.add(Projections.property("col1"), "col1");
projectionList2.add(Projections.property("col2"), "col2");
criteria.setProjection(projectionList2);
criteria.setResultTransformer(Transformers.aliasToBean(Test.class)); 
List list = criteria.list();

In you case, "distinctColumn" will be "name".

we had the same problem, Creating an alias solved the issue, try:

Criteria criteria = session.createCriteria(A.class, "a")
criteria.add(Restrictions.isNotNull("a.name"));
ProjectionList list = Projections.projectionList();
list.add(Projections.distinct(Projections.property("a.name")),"name");
criteria.setProjection(list);
criteria.setResultTransformer(Transformers.aliasToBean(A.class));
result = getHibernateTemplate().findByCriteria(criteria);

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