gpt4 book ai didi

java - Hibernate 似乎形成了错误的查询

转载 作者:行者123 更新时间:2023-12-01 04:46:24 25 4
gpt4 key购买 nike

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

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

错误是错误 - 列名“y0_”无效我不明白为什么 hibernate 会形成这样的错误查询。任何帮助表示赞赏。

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

}

最佳答案

尝试以下操作:

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”将是“name”。

关于java - Hibernate 似乎形成了错误的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15782171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com