why EntityManager is null?

前端 未结 3 1730
-上瘾入骨i
-上瘾入骨i 2021-01-05 00:32

In my web applicaton I use OpenJPA on Apache Tomcat (TomEE)/7.0.37 server. I use Netbeans to auto generate class (\"Entity Class from database...\" and \"Session Beans From

相关标签:
3条回答
  • 2021-01-05 01:01

    The persistence.xml file should be under META-INF folder. Check this documentation to understand the structure.

    0 讨论(0)
  • 2021-01-05 01:02

    I was facing the same problem, as i am using Jersey Rest with the JPA/Hibernate and Spring,in my project, and getting entity manager as null everytime while using

    @PersistenceContext(name = "JPA_DEMO", type = PersistenceContextType.TRANSACTION)
     EntityManager em;
    

    and if i was creating it manually like the syntax below it is working fine.

    EntityManagerFactory ENTITY_MANAGER_FACTORY = Persistence.createEntityManagerFactory("JPA_DEMO");//
    EntityManager em = ENTITY_MANAGER_FACTORY.createEntityManager();
    

    After some research i found the problem is, that though we are using the @PersistenceContext who is responsible to inject the entity manager object, but it doesn't had the class from which it will get the object i.e the Class which is used by the Spring to create and get the Entity Manager Object from and inject it to our EntityManager object defined under @PersistenceContext. The responsible class is LocalContainerEntityManagerFactoryBean which is defined in my application.xml(used for loading my spring beans). So i defined it and it worked. Below is the syntax for defining it and it is for the transaction manager, so u can use transactions also.

    <tx:annotation-driven transaction-manager="transactionMgr" />
    
    <bean id="transactionMgr" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="MgrFactory"/>
    </bean>
    
        <bean id="MgrFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="com.restDemo"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
                <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            </props>
        </property>
    </bean>
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    
    0 讨论(0)
  • 2021-01-05 01:28

    To get not null EntityManager from

    @PersistenceContext(unitName = "CollDocPU")
    private EntityManager em;
    

    i have to change my persistance.xml, change transaction-type to "JTA" and add:

    <jta-data-source>java:openejb/Resource/myDatabase</jta-data-source>
    <non-jta-data-source>java:openejb/Resource/myDatabaseUnmanaged</non-jta-data-source>
    

    after that, i have to declare resources in my server configuration: at [tomee installation folder]/conf/tomee.xml file:

    <?xml version="1.0" encoding="UTF-8"?>
     <tomee>
      <Resource id="myDatabase" type="DataSource">
         JdbcDriver com.mysql.jdbc.Driver
         JdbcUrl jdbc:mysql://localhost:11080/jkitaj?zeroDateTimeBehavior=convertToNull
         UserName jkitaj
         Password pass,
      </Resource>
    
      <Resource id="myDatabaseUnmanaged" type="DataSource">
         JdbcDriver com.mysql.jdbc.Driver
         JdbcUrl jdbc:mysql://localhost:11080/jkitaj?zeroDateTimeBehavior=convertToNull
         UserName jkitaj
         Password pass,
         JtaManaged false
      </Resource>
     </tomee>
    

    Look there:

    http://openejb.979440.n4.nabble.com/org-apache-openjpa-lib-jdbc-ReportingSQLException-type-not-found-or-user-lacks-privilege-td4665124.html

    http://mobiarch.wordpress.com/2012/12/07/configuring-a-mysql-data-source-in-tomee/

    0 讨论(0)
提交回复
热议问题