当使用criteria.createAlias()进行别名设置的时候,返回的对象是Object[]数组。并且和接受类型不匹配它也不会报错。
代码:
@Override
public SearchResult<POIError> findListByManyCondition(Integer pageNo, Integer size, String errorType,
String keyword, String sortProperty, String asc) {
Criteria criteria = createCriteria();
if (errorType != null && errorType != "") {
criteria.add(Restrictions.eq("type", Integer.valueOf(errorType)));
}
criteria.createAlias("submitter", "submitter");
criteria.createAlias("category", "category");
if (keyword != null && keyword != "") {
keyword="%"+keyword+"%";
criteria.add(Restrictions.or(Restrictions.like("submitter.userName", keyword),Restrictions.like("category.name", keyword)));
}
if (sortProperty != null&&new Boolean(asc) == false) {
if (sortProperty.equals("id")) {
criteria.addOrder(Order.desc("id"));
}
if (sortProperty.equals("poi")) {
criteria.addOrder(Order.desc("poiId"));
}
if (sortProperty.equals("member.name")) {
criteria.addOrder(Order.desc("submitter.userName"));
}
if (sortProperty.equals("contant")) {
criteria.addOrder(Order.desc("type"));
}
if (sortProperty.equals("createDate")) {
criteria.addOrder(Order.desc("createDate"));
}
if (sortProperty.equals("status")) {
criteria.addOrder(Order.desc("operationStatus"));
}
}else {
if (sortProperty.equals("id")) {
criteria.addOrder(Order.asc("id"));
}
if (sortProperty.equals("poi")) {
criteria.addOrder(Order.asc("poiId"));
}
if (sortProperty.equals("member.name")) {
criteria.addOrder(Order.asc("submitter.userName"));
}
if (sortProperty.equals("contant")) {
criteria.addOrder(Order.asc("type"));
}
if (sortProperty.equals("createDate")) {
criteria.addOrder(Order.asc("createDate"));
}
if (sortProperty.equals("status")) {
criteria.addOrder(Order.asc("operationStatus"));
}
}
long count=(Long)criteria.setProjection(Projections.rowCount()).uniqueResult();
List<POIError> datas=criteria.setProjection(null).setFirstResult((pageNo - 1) * size).setMaxResults(size).list();
return new SearchResult(count, datas);
}
返回的时候一直返回的Object[]数组,就有点纳闷了。数组里面是相关别名实体那一行的数据,感觉有点奇怪。
不管设置成什么方式都一样,而且根本不报错。
源码:
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.sql;
import org.hibernate.HibernateException;
/**
* @author Strong Liu
*/
public enum JoinType {
NONE( -666 ),
INNER_JOIN( 0 ),
LEFT_OUTER_JOIN( 1 ),
RIGHT_OUTER_JOIN( 2 ),
FULL_JOIN( 4 );
private int joinTypeValue;
JoinType(int joinTypeValue) {
this.joinTypeValue = joinTypeValue;
}
public int getJoinTypeValue() {
return joinTypeValue;
}
public static JoinType parse(int joinType) {
if ( joinType < 0 ) {
return NONE;
}
switch ( joinType ) {
case 0:
return INNER_JOIN;
case 1:
return LEFT_OUTER_JOIN;
case 2:
return RIGHT_OUTER_JOIN;
case 4:
return FULL_JOIN;
default:
throw new HibernateException( "unknown join type: " + joinType );
}
}
}
但是心想肯定会有一种方式可以设置只取主表的数据。
最后在源码看到了这个public static final ResultTransformer ROOT_ENTITY = RootEntityResultTransformer.INSTANCE;在CriteriaSpecification类里面。
然后把它设置进去。
修改后的代码:
List<POIError> datas=criteria.setProjection(null).setResultTransformer(Criteria.ROOT_ENTITY).setFirstResult((pageNo - 1) * size).setMaxResults(size).list();
这样取出来就是我们想要的那个对象了。就不是数组了,所以我们还要再c.setResultTransformer(Criteria.ROOT_ENTITY);把结果以Entity的形式返回,而不是Object[]的形式返回。