我从Spring MVC和Hibernate开始。
系统正确部署在服务器中,但我有以下例外:
Advertencia: StandardWrapperValve[dispatcher]: Servlet.service() for servlet dispatcher threw exception
java.lang.NullPointerException
at modelo.RefaccionesDAO.consultarTodo(RefaccionesDAO.java:30)
at modelo.RefaccionesDAO$$FastClassByCGLIB$$b57106b2.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:710)
...
执行此代码时会发生这种情况:
@Service //ESTE ES A FUERZAS.
public class RefaccionesDAO {
@PersistenceContext //LAS OPERACIONES POR DEFECTO.
private EntityManager em;
//CONTROLA LAS EXEPCIONES.
@Transactional(rollbackFor = {ServicioException.class})
public Refaccion consultarTodo(){
em.find(Refaccion.class, "10");
return new Refaccion(1000, "Rafael Angel", 0, 0, "xdafj", "sldfljdf", true);
}
}
具体的一行是:em.find(Refaccion.class, "10");
em
与返回分开以进行调试,因为我想在Refaccion
我的配置
的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="UNIDAD_DE_PERSISTENCIA_PU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:app/BASE_DE_DATOS_PRUEBAS</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
aplicationContext.xml
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-4.0.1.xsd">
<jee:jndi-lookup id="myEmf" jndi-name="persistence/UNIDAD_DE_PERSISTENCIA_PU" />
<!-- //PERMITE LAS TRANSACCIONES JPA.-->
<tx:jta-transaction-manager />
<context:component-scan base-package="controller" />
<context:component-scan base-package="entidades" />
<context:component-scan base-package="modelo" />
</beans>
调度-servlet.xml中
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<tx:annotation-driven /> <!-- Esto es por que vamos a usar MVC de SPRING -->
<mvc:annotation-driven/> <!-- Nos permite trabajar con transacciones JPA -->
<context:annotation-config />
<!-- Esto escanea los paquetes con los que vamos a trabajar. -->
<context:component-scan base-package="controller" />
<context:component-scan base-package="entidades" />
<context:component-scan base-package="modelo" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<mvc:view-controller path="/otro.htm" view-name="otro"/>
<!-- <mvc:resources location="/" mapping="/static/**" />-->
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
这是我打电话给mi RefaccionDao
@Controller
public class TaponesController {
@Autowired
private RefaccionesDAO dao;
@RequestMapping(value = "/tapones/guardar", method = RequestMethod.POST)
@ResponseBody
public String guardarTapon(@RequestParam HashMap<String, String> datos, ModelMap model){
String text = " testo normal xP : ";
Refaccion r= dao.consultarTodo();
text = text +" "+ r.getNombre();
return text;
}
}
为此更改此@PersistenceContext
:
@PersistenceContext(unitName = "UNIDAD_DE_PERSISTENCIA_PU", type = PersistenceContextType.EXTENDED)
private EntityManager em;
对此进行更改:@PersistenceContext(unitName = "UNIDAD_DE_PERSISTENCIA_PU", type = PersistenceContextType.EXTENDED)
:
@PersistenceContext(unitName = "UNIDAD_DE_PERSISTENCIA_PU")
private EntityManager em;
没有任何改变。
答案 0 :(得分:0)
我解决了这个问题。我将其添加到pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
现在正在运作。