why EntityManager is null?
Asked Answered
Z

3

9

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 Entity Class..."). At SessionBean (for example 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 by above way I get null. When I make it by:

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

ecm is not null and it ok

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>
Zacynthus answered 17/9, 2013 at 9:56 Comment(4)
Are you sure EJB support works in your tomcat environment? How do you get instance of UserFacade?Kerato
Share your application structure . I would like to check where is your persistence.xml is locatedBessie
@Kerato - i get UserFacade in @ManagedBean public class RegisterController private String registerLog = ""; private String registerPass = ""; private @EJB UserFacade userFacade;Zacynthus
@Gaurav: [link] wstaw.org/w/2eYIZacynthus
Z
5

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/

Zacynthus answered 23/9, 2013 at 10:3 Comment(4)
your links are not of help. Can you mention some other linksNicotine
@AbhishekRanjan what is your problem, mayby I can help you? Do you ask quation about it?Zacynthus
I am using apache Tomee plus. I need to declare JTA datasource but I dont know where to save its properties.Nicotine
Do you talk about file like my persistence.xml (look to my question above) or about tomee.xml in tomee server configuration?Zacynthus
M
2

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>
Muggins answered 4/12, 2018 at 11:12 Comment(0)
B
0

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

Bessie answered 17/9, 2013 at 10:25 Comment(2)
I move persistence.xml under META-INF folder, but it doesn't help. Entity Manager is null when I get it by @PersistenceContext(unitName = "CollDocPU") private EntityManager em;Zacynthus
@PersistenceContext(unitName = "CollDocPU") private EntityManagerFactory emf; too make emf null :(Zacynthus

© 2022 - 2024 — McMap. All rights reserved.