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

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

}

最佳答案

尝试这个

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

10-04 23:01