SQLState: S0022

SQLState: S0022

 

	StringBuffer sb = new StringBuffer();
		sb.append(" select  {m.*}   from d_provider_to_factory as d , m_enterprice  as m where  1=1 ");
		if ("0".equals(enterpriceType)) {
			sb.append(" and  d.provider_id = m.enterprice_id ");
		}else if("1".equals(enterpriceType)){
			sb.append(" and d.factory_id = m.enterprice_id ");
		}
		sb.append(" and  m.flag_del = '0' ");
		sb.append(" and  d.flag_del = '0' ");
		
		if ("0".equals(enterpriceType)) {
			sb.append(" group by   d.provider_id  ");
		}else if("1".equals(enterpriceType)){
			sb.append(" group by  d.factory_id ");
		}
		
		if (page <=0) {
			page = 1;
		}
		
		String sql = sb.toString();
		sb = null;
		Session session = getSession();
	    List  pList = session.createSQLQuery(sql)
			.addEntity("d", DProviderToFactory.class)
			.addEntity("m",MEnterprice.class)
			.setFirstResult((page-1)*pageSize)
			.setMaxResults(pageSize)
			.list();

 

执行的时候 报错:

Hibernate: 
    select
        m.enterprice_id as enterprice1_84_1_,
        m.enterprice_name as enterprice2_84_1_,
        m.enterprice_short_name as enterprice3_84_1_,
        m.enterprice_original_name as enterprice4_84_1_,
        m.parent_enterprice_id as parent5_84_1_,
        m.trade_type as trade6_84_1_,
        m.province_id as province7_84_1_,
        m.administrative_division as administ8_84_1_,
        m.address as address84_1_,
        m.post_code as post10_84_1_,
        m.enterprise_type as enterprise11_84_1_,
        m.corporate_representative as corporate12_84_1_,
        m.fax as fax84_1_,
        m.phone_number as phone14_84_1_,
        m.cell_phone_number as cell15_84_1_,
        m.email as email84_1_,
        m.begin_date as begin17_84_1_,
        m.register_capital as register18_84_1_,
        m.operate_begin_date as operate19_84_1_,
        m.operate_end_date as operate20_84_1_,
        m.register_department as register21_84_1_,
        m.tax_number as tax22_84_1_,
        m.register_code as register23_84_1_,
        m.operate_area as operate24_84_1_,
        m.remark as remark84_1_,
        m.flag_del as flag26_84_1_,
        m.create_datetime as create27_84_1_,
        m.create_date_user as create28_84_1_,
        m.update_datetime as update29_84_1_,
        m.update_date_user as update30_84_1_   
    from
        d_provider_to_factory as d ,
        m_enterprice  as m 
    where
        1=1  
        and d.factory_id = m.enterprice_id  
        and  m.flag_del = '0'  
        and  d.flag_del = '0'  
    group by
        d.factory_id 
2011-07-06 15:44:29,595 WARN [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 0, SQLState: S0022
2011-07-06 15:44:29,595 ERROR [org.hibernate.util.JDBCExceptionReporter] - Column 'provider1_83_0_' not found.
org.hibernate.exception.SQLGrammarException: could not execute query
	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
	at org.hibernate.loader.Loader.doList(Loader.java:2223)
	at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
	at org.hibernate.loader.Loader.list(Loader.java:2099)
	at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
	at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
	at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
	at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
	at cn.com.jdlh.enterprice.dao.EnterPriceDAO.getEnterPriceList(EnterPriceDAO.java:62)
	at cn.com.jdlh.enterprice.dao.EnterPriceDAO$$FastClassByCGLIB$$4c9aae10.invoke(<generated>)
	at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
	at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:700)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
	at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635)
	at cn.com.jdlh.enterprice.dao.EnterPriceDAO$$EnhancerByCGLIB$$dffb0954.getEnterPriceList(<generated>)
	at cn.com.jdlh.enterprice.action.EnterPriceAction.enterPriceList(EnterPriceAction.java:30)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440)
	at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
	at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:163)
	at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:249)
	at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
	at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
	at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
	at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:148)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:93)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:128)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:104)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
	at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:148)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:128)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
	at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
	at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:468)
	at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:242)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:102)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:242)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at filters.SecurityFilter.doFilter(SecurityFilter.java:105)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:242)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:242)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:201)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:563)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:163)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:556)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:401)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:242)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:267)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:245)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:260)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
	at java.lang.Thread.run(Thread.java:619)
Caused by: java.sql.SQLException: Column 'provider1_83_0_' not found.
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
	at com.mysql.jdbc.ResultSet.findColumn(ResultSet.java:987)
	at com.mysql.jdbc.ResultSet.getInt(ResultSet.java:2749)
	at com.mchange.v2.c3p0.impl.NewProxyResultSet.getInt(NewProxyResultSet.java:2573)
	at org.hibernate.type.IntegerType.get(IntegerType.java:28)
	at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
	at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154)
	at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1097)
	at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:565)
	at org.hibernate.loader.Loader.doQuery(Loader.java:701)
	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
	at org.hibernate.loader.Loader.doList(Loader.java:2220)
	... 95 more

 

 

 

我将 Hibernate 打印出来的SQL 拿出来可以单独执行,但是 放到程序里就出错了。

 

 

最后找到问题根源

 

 

   List pList = session.createSQLQuery(sql)
			//.addEntity("d", DProviderToFactory.class)
			.addEntity("m",MEnterprice.class)
			.setFirstResult((page-1)*pageSize)
			.setMaxResults(pageSize)
			.list();
	    

 

因为 我不需要 查询出 d 实体的内容 所以 就不用吧 d也实例化 将

//.addEntity("d", DProviderToFactory.class)

 

注释掉 就可以了。

 

如果不注释掉 也行 就修改sql 语句

 

StringBuffer sb = new StringBuffer();
		sb.append(" select  {m.*} ,{d.*}  from d_provider_to_factory as d , m_enterprice  as m where  1=1 ");
		if ("0".equals(enterpriceType)) {
			sb.append(" and  d.provider_id = m.enterprice_id ");
		}else if("1".equals(enterpriceType)){
			sb.append(" and d.factory_id = m.enterprice_id ");
		}
		sb.append(" and  m.flag_del = '0' ");
		sb.append(" and  d.flag_del = '0' ");
		
		if ("0".equals(enterpriceType)) {
			sb.append(" group by   d.provider_id  ");
		}else if("1".equals(enterpriceType)){
			sb.append(" group by  d.factory_id ");
		}
		
		if (page <=0) {
			page = 1;
		}
		
		String sql = sb.toString();
		sb = null;
		Session session = getSession();
	    List  pList = session.createSQLQuery(sql)
		.addEntity("d", DProviderToFactory.class)
		.addEntity("m",MEnterprice.class)
		.setFirstResult((page-1)*pageSize)
		.setMaxResults(pageSize)
		.list();
	    

 

 

总结:如果非要用hibernate查询sql 语句并且进行自动映射的话,最好用到那个实体就添加那个实体。

你可能感兴趣的:(sql)