1

我的要求是提取名称不为空的所有不同行(仅名称列)这是我的休眠代码。

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

形成的SQL如下:

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

错误是ERROR - Invalid column name 'y0_' 我不明白为什么休眠会形成这样的错误查询。任何帮助表示赞赏。

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

}
4

3 回答 3

0

尝试这个

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));
于 2013-04-03T09:35:29.323 回答
0

尝试以下:

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();

在您的情况下,“distinctColumn”将是“名称”。

于 2013-04-03T12:03:41.083 回答
0

我们遇到了同样的问题,创建别名解决了这个问题,尝试:

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);
于 2015-04-29T15:46:32.270 回答