DetachedCriteria 的Expression.in
<hibernate-mapping>
<class name="com.quanguowuliu.hibernate.pojo.CompanyUserInfo" table="companyUserInfo" schema="dbo" catalog="logistic" lazy="false">
<id name="uid" type="java.lang.Integer">
<column name="Uid" />
<generator class="identity" />
</id>
<many-to-one name="companyTypeInfo" class="com.quanguowuliu.hibernate.pojo.CompanyTypeInfo" fetch="select">
<column name="companyTypeId" />
</many-to-one>
<many-to-one name="areaInfo" class="com.quanguowuliu.hibernate.pojo.AreaInfo" lazy="false" fetch="select">
<column name="areaBid" />
</many-to-one>
</class>
</hibernate-mapping>
我写了一个方法
查询某个省的公司,但是每个公司引用的是市区编号
我是这样写的
int count=0;
int pageindex = 1;// 页码
int pagesize = 1;// 页面大小
String type=request.getParameter("type");
request.setAttribute("type", type);
DetachedCriteria c=DetachedCriteria.forClass(CompanyUserInfo.class);
if (request.getParameter("page") != null) {
pageindex = Integer.parseInt(request.getParameter("page"));
}
if(request.getParameter("shengId")!=null){
int id=Integer.parseInt(request.getParameter("shengId"));
//查询市
List<AreaInfo> areaInfoList=areaInfoService.getAreaInfo(id);
c.add(Expression.in("areaInfo.areaBid", areaInfoList));
request.setAttribute("shengId", id);
}
打印出来的语句是这样的
Hibernate:
/* criteria query */ select
this_.Uid as Uid142_0_,
this_.companyTypeId as companyT2_142_0_,
this_.areaBid as areaBid142_0_,
this_.Uname as Uname142_0_,
this_.Upwd as Upwd142_0_,
this_.companyName as companyN6_142_0_,
this_.companyType as companyT7_142_0_,
this_.Birthday as Birthday142_0_,
this_.enrolMoney as enrolMoney142_0_,
this_.personnelCount as personn10_142_0_,
this_.corporativePerson as corpora11_142_0_,
this_.Address as Address142_0_,
this_.type as type142_0_,
this_.Phone as Phone142_0_,
this_.Summary as Summary142_0_,
this_.Mobile as Mobile142_0_,
this_.Image as Image142_0_,
this_.Email as Email142_0_,
this_.InternetSite as Interne19_142_0_,
this_.Fax as Fax142_0_,
this_.premeditation1 as premedi21_142_0_,
this_.premeditation2 as premedi22_142_0_
from
logistic.dbo.companyUserInfo this_
where
this_.type=?
and this_.areaBid in (
?, ?
)
但是出现了异常
could not bind value '[Ljava.lang.Object;@2c5f8a' to parameter: 2; [Ljava.lang.Object;
- Unhandled Exception thrown: class java.lang.ClassCastException
2008-10-23 9:46:25 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet action threw exception
java.lang.ClassCastException: [Ljava.lang.Object;
at org.hibernate.type.IntegerType.set(IntegerType.java:41)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:83)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:65)
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1698)
at org.hibernate.loader.Loader.bindParameterValues(Loader.java:1669)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1554)
at org.hibernate.loader.Loader.doQuery(Loader.java:661)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2211)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
at org.hibernate.loader.Loader.list(Loader.java:2090)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:95)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at org.springframework.orm.hibernate3.HibernateTemplate$35.doInHibernate(HibernateTemplate.java:979)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:367)
at org.springframework.orm.hibernate3.HibernateTemplate.findByCriteria(HibernateTemplate.java:969)
at org.springframework.orm.hibernate3.HibernateTemplate.findByCriteria(HibernateTemplate.java:962)
at com.quanguowuliu.dao.BaseDAO.ListDeptInfor(BaseDAO.java:43)
at com.quanguowuliu.service.imp.CompanyUserInfoService.ListDeptInfor(CompanyUserInfoService.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:281)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:187)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:154)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:210)
at $Proxy17.ListDeptInfor(Unknown Source)
at com.quanguowuliu.face.struts.action.CompanyUserAction.CheckList(CompanyUserAction.java:114)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:274)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:194)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
请各位大虾帮哈忙,我的邮箱是zhouquan516@gmail.com,qq是295472899
[解决办法]
List <AreaInfo> areaInfoList=areaInfoService.getAreaInfo(id);
c.add(Expression.in("areaInfo.areaBid", areaInfoList));
查询参数类型不匹配,
areaInfoList是一个对象的List< areaInfo >, 里面的类型应该是areaInfo.areaBidd的类型. 应该重新建一个list, 里面的元素是areaInfo中的id.