I ran into the same problem since I use Jersey Rest with JPA / Hibernate and Spring in my project and get entity control as null every time I use
@PersistenceContext(name = "JPA_DEMO", type = PersistenceContextType.TRANSACTION) EntityManager em;
and if I created it manually, as the syntax below, it works fine.
EntityManagerFactory ENTITY_MANAGER_FACTORY = Persistence.createEntityManagerFactory("JPA_DEMO");
After some research, I found that the problem is that although we use @PersistenceContext, which is responsible for entering the entity control object, it does not have the class from which it will receive the object, i.e. The class that Spring uses is to create and receive an Entity Manager object and paste it into our EntityManager object defined in @PersistenceContext. The responsible class is LocalContainerEntityManagerFactoryBean, which is defined in my application.xml application (used to load my spring beans). So I spotted it, and it worked. Below is the syntax for defining it, and it is intended for the transaction manager, so you can also use transactions.
<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>
source share