This question has already been answered:
I get by id but by sql or hql and the criteria goes NPE.and when displayed in jsp instead of letters question marks .. I tried to configure the server.xml file in tomcat in UTF-8, src, jsp everything in UTF-8.spring 4.1. 1 hibernate 4.3.5. Help please. Already the third day I suffer. and here is the code.
**MAIN.JS** $('#button2').click( function(){ $.ajax({ url:"getcity", type : "GET", contentType:"application/text", success:function(City){ if(City!=null){ $('#result').html(City); }else{$('#result').html("ОШИБКААААААА!!!!!");} } }) }) **CONTROLLER** @ResponseBody @RequestMapping(value = "/getcity",method = RequestMethod.GET) public String getCityPoSql() { cities cit = cityService.getCity(); return"<h1>" + cit.toString() + "</h1>"; } } **DAO** @Transactional(readOnly = true) public cities getCity() { Criteria criteria = sessionFactory.getCurrentSession().createCriteria(cities.class); criteria.add(eq("city", "Москва")); cities cit =(cities) criteria.uniqueResult(); return cit ; } }
cityService is not null..and getCity returns null..base in UTF-8 /// config sessionFactory ..
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url"> <value>jdbc:mysql://localhost:3306/cities</value> </property> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan" value="com.plan.model"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean>
If in criteria query here so to write: actually so
@Transactional(readOnly = true) public cities getCityIdSql() { Criteria criteria = sessionFactory.getCurrentSession().createCriteria(cities.class); cities cit =(cities) criteria.add(Restrictions.eq("city","Москва")).uniqueResult(); return cit ; } }
whether it should return all other fields where city is Moscow .. in my case it is a string and returns NPE. there is such a field and the value is
**Stacktrace** org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) root cause java.lang.NullPointerException com.plan.pn.cityController.getCityPoSql(cityController.java:42) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:606) org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) note The full stack trace of the root cause is available in the Apache Tomcat/7.0.14 logs. Apache Tomcat/7.0.14