今天发现一个奇怪的问题,在SQLSERVER2005下有个表中字段类型为nvarcha,在hibernate中HQL语句查询个别中文字符总是查不到但是表中确实有这些数据的,后来用SQL直接查询
select * from CustomerDataTbl where custname like '%咣咣%'
还是查不到,后来改成
select * from CustomerData where custname like N'%咣咣%'
就正常了,查了资料nvarchar是unicode类型储存,所以查询时要以N来标示当前参数对应为unicode,但现在的问题是,这在hibernate中用HQL怎么写呢,我尝试 from CustomerData a where a.custName like N'%咣咣%' 来执行,但是一直报错ERROR [http-bio-8080-exec-10] PARSER.reportError(56) | line 1:77: unexpected token: '???'
ERROR [http-bio-8080-exec-10] ExceptionMappingInterceptor.error(27) | ERROR:
org.springframework.orm.hibernate3.HibernateQueryException: unexpected token: '???' near line 1, column 77 [ from com.ert.model.pes.basedata.CustomerData a where 1=1  and a.custName =N'???']; nested exception is org.hibernate.hql.ast.QuerySyntaxException: unexpected token: '???' near line 1, column 77 [ from com.ert.model.pes.basedata.CustomerData a where 1=1  and a.custName =N'???']
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:660)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.convertHibernateAccessException(AbstractSessionFactoryBean.java:303)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.translateExceptionIfPossible(AbstractSessionFactoryBean.java:282)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy144.queryList(Unknown Source)
at com.ert.service.impl.pes.basedata.CustomerDataManagerImpl.getRecordsPageByConditions(CustomerDataManagerImpl.java:62)
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 org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy145.getRecordsPageByConditions(Unknown Source)
at com.ert.webapp.action.pes.basedata.CustomerDataAction.search(CustomerDataAction.java:93)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)求教哪位大侠指点迷津,不胜感激啊!!!!!