Why is EntityManager NULL?

In my web application, I use OpenJPA on Apache Tomcat (TomEE) /7.0.37 server. I use Netbeans to automatically create a class ("Entity Class from database ..." and "Session Beans From Entity Class ..."). In SessionBean (e.g. UserFacade) I want to get EntityManager:

@Stateless public class UserFacade extends AbstractFacade<User> { @PersistenceContext(unitName = "CollDocPU") private EntityManager em; @Override protected EntityManager getEntityManager() { return em; } } 

but when i get it higher i get null. When I do this:

 @Override protected EntityManager getEntityManager() { EntityManagerFactory emf = Persistence.createEntityManagerFactory("CollDocPU"); EntityManager ecm = emf.createEntityManager(); return ecm; } 

ecm is not null and that's fine

my persistence.xml:

 <?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="CollDocPU" transaction-type="RESOURCE_LOCAL"> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> <class>model.entity.StudentAddSolution</class> <class>model.entity.Lecturer</class> <class>model.entity.Solution</class> <class>model.entity.Student</class> <class>model.entity.Course</class> <class>model.entity.File</class> <class>model.entity.CourseHasLecturer</class> <class>model.entity.Mail</class> <class>model.entity.StudentAtCourse</class> <class>model.entity.Roles</class> <class>model.entity.Task</class> <class>model.entity.User</class> <exclude-unlisted-classes>false</exclude-unlisted-classes> <properties> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:11080/myBase?zeroDateTimeBehavior=convertToNull"/> <property name="javax.persistence.jdbc.password" value="pass,"/> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> <property name="javax.persistence.jdbc.user" value="myBase"/> <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/> </properties> </persistence-unit> </persistence> 
+7
source share
3 answers

To get non null EntityManager from

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

I need to change my persistance.xml, change the 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 the resources in my server configuration: in the installation folder [tomee installation] /conf/tomee.xml:

 <?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> 

Take a 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/

+2
source

The persistence.xml file must be in the META-INF folder. this documentation to understand the structure.

0
source

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");// EntityManager em = ENTITY_MANAGER_FACTORY.createEntityManager(); 

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> 
0
source

Source: https://habr.com/ru/post/953974/


All Articles