why EntityManager is null?

旧街凉风 提交于 2019-11-30 20:08:44

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/

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>

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!