2017-12-14 14 views
0

私はSpring MVCとHibernateから始まります。Spring mvcとhibernateを持つEntityManagerのjava.lang.NullPointerException

システムが正常にサーバーに展開しますが、私は次の例外があります

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); 
    } 
} 

especific行はこれです:em.find(Refaccion.class, "10");

emは、デバッガ用のリターンから分離されています。なぜなら、discarにエラーがあります。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> 
ここでは3210

ディスパッチャ-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> 

私は、私が試してみました何マイル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

あなたのentitymanagerは注入されません。私があなただったら、簡単にSpringデータを使用します。 hibernateをタグとして追加しますが、JPA実装として 'org.eclipse.persistence.jpa.PersistenceProvider'を使用します。そう ?? – akuma8

+0

@ akuma8私は、休止状態を使うと仮定していました。それは私の最初のプロジェクトであり、私はあまり理解していません。私は 'org.hibernate.ejb 'の行を変更しました。HibernatePersistence'を呼び出していますが、新しいエラーが発生しました: 'org.glassfish.persistence.jpa.JPADeployerクラスを呼び出す際に例外が発生しました Grave:java.lang.RuntimeException:java.lang.ClassNotFoundException:org.hibernate.ejb.HibernatePersistence' – Angel

答えて

0

私はこの問題を解決しました。これをpom.xmlに追加します

<dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-orm</artifactId> 
     <version>${spring.version}</version> 
</dependency> 

現在作業中です。

関連する問題