2017-11-22 9 views
0

私はいつもnullを取得していますが、私はその理由を知りません。私は解決策を探すのに疲れているが、見つけられなかった。私はSpring 5.0.1でjpaを注入しようとしているので、コードの一部を入れます。EntityManagerは常にnullです

のpersistence.xml(META-INFディレクトリ内)

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.2.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 

<!-- para que reconozca los Servicios, DAOs, etc anotados --> 
<!--<context:component-scan base-package="ttps.daosjpa" /> --> 
<context:component-scan base-package="Controlador,DAO,Modelo" /> 

<!-- propiedades de la Base de Datos 
<context:property-placeholder location="classpath:database.properties" />--> 

<!-- DataSource --> 
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 
    <property name="driverClass" value="com.mysql.jdbc.Driver" /> 
    <property name="jdbcUrl" value="jdbc:mysql://localhost/ttps2017" /> 
    <property name="user" value="root" /> 
    <property name="password" value="" /> 
    <property name="acquireIncrement" value="2" /> 
    <property name="minPoolSize" value="20" /> 
    <property name="maxPoolSize" value="50" /> 
    <property name="maxIdleTime" value="600 " /> 
</bean> 


<!-- Configuración JPA --> 
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <!-- <property name="packagesToScan" value="ttps.springmvc.entities" /> --> 
    <property name="packagesToScan" value="Controlador,DAO,Modelo" /> 

    <property name="jpaVendorAdapter"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> 
    </property> 
    <property name="jpaProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 
      <prop key="hibernate.hbm2ddl.auto">update</prop> 
      <prop key="hibernate.format_sql">true</prop> 
      <prop key="hibernate.show_sql">true</prop> 
     </props> 
    </property> 
</bean> 


<!-- Manejador de Transacciones --> 

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager "> 
    <property name="entityManagerFactory" ref="emf" /> 
</bean> 
<tx:annotation-driven transaction-manager="transactionManager" /> 

これは私のgenericDAOHibernateです..................... .................................................. .................................................. .................................................. .................................................. ......................

@Transactional 
public class GenericDAOHibernate<T> implements GenericDAO<T> { 

    private Class<T> PersistentClass; 
    private EntityManager entityManager; 

    @PersistenceContext 
    public void setEntityManager(EntityManager em) { 
     this.entityManager= em; 
    } 

    public EntityManager getEntityManager() { 
     return this.entityManager; 
    } 


    public GenericDAOHibernate(Class<T> entidad) { 
     this.PersistentClass = entidad; 
    } 

    @Override 
    public T persistir(T entity) { 
     System.out.println(""); 
     System.out.println(this.getEntityManager()); 
     System.out.println(""); 
     this.getEntityManager().persist(entity); 
     return entity; 
    } 
} 

これは私ですweb.xmlの

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
version="3.0"> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/META-INF/newPersistence.xml</param-value> 
</context-param> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<!-- Configuration for the DispatcherServlet 
<servlet> 
    <servlet-name>spring</servlet-name> 
     <servlet-class> 
      org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>spring</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> --> 

答えて

0

は、私はここで、ドットの代わりにコンマされるべきだと思います。

<context:component-scan base-package="Controlador.DAO.Modelo" /> 
+0

はい、投稿後に変更しましたが、それは問題の一部ではないと思います。私は何かがXMLには、Eclipseによると、コードに "エラー"がないと言われていると思います。ありがとうございます。 –

関連する問題