Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why EntityManager is null?

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>
like image 247
kuba44 Avatar asked Sep 17 '13 09:09

kuba44


1 Answers

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/

like image 161
kuba44 Avatar answered Sep 20 '22 01:09

kuba44