我在我的dao中查询了这个问题
Query q = entityManager.createQuery("SELECT geoCity,(CASE WHEN geoCity.name LIKE :search1 THEN 0 ELSE 1 END) as myOrder FROM GeoCity geoCity WHERE (LOWER(geoCity.name) LIKE :search ) ORDER BY geoCity.myOrder ,geoCity.name");
这是实体
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
/**
* GeoCity generated by hbm2java
*/
@Entity
@Table(name="geo_city" ,schema="public")
public class GeoCity implements java.io.Serializable {
.....
public transient Integer myOrder;
public GeoCity() {
}
public GeoCity(String istat) {
this.istat = istat;
}
@Transient
public Integer getMyOrder() {
return myOrder;
}
public void setMyOrder(Integer myOrder) {
this.myOrder = myOrder;
}
}
geoRegion.myOrder无效。但我需要在我的查询内部使用案例以特定的方式进行排序。有办法吗?
答案 0 :(得分:0)
这是因为您的声明是using BST::BST
。
transient
尝试删除public transient Integer myOrder; // this is transient
。
transient
并删除方法中的注释public Integer myOrder;
。
瞬态表示,您声明的变量不 @Transient
这就是为什么它是hibernate中的无效输入。点击此链接可获取 Transient Keyword 。
答案 1 :(得分:0)
我使用原生查询发布替代解决方案
public ArrayList<Autocomplete> autocomplete(String search, HttpSession httpSession, HttpServletRequest request) {
ArrayList<Autocomplete> autocomplete = new ArrayList<>();
try{
Query q = entityManager.createNativeQuery("SELECT istat,name,CASE WHEN LOWER(g.name) LIKE :search1 THEN 0 ELSE 1 END as myOrder FROM geo_city g WHERE (LOWER(g.name) LIKE :search ) ORDER BY myOrder, g.name");
q.setParameter("search","%" + search.toLowerCase() + "%");
q.setParameter("search1",search.toLowerCase() + "%");
List<Object[]> city = q.getResultList();
if(city!=null && city.size()>0){
for(Object[] o: city) {
autocomplete.add(new Autocomplete(o[0].toString(), o[1].toString()));
}
}else{
autocomplete.add(new Autocomplete("","Nessun risultato!"));
}
}catch(Exception e){
e.printStackTrace();
}
return autocomplete;
}