EntityManagerFactory.createEntityManagerFactory(String) throws strange NullPointerException
margul Aug 5, 2012 11:17 AMHi everybody,
I ran into a problem, for which I couldn't find a solution th whole day.
I'm developing a 3-tier application for JBoss 7.1.1, and from the logic tier, I want to call a stateless session bean, which accesses my MySQL DB via JPA and Hibernate.
A logic bean calls the dao bean via JNDI, which works perfectly:
{code}
package logic;
import dao.vehicle.VehicleDAOLocal;
import dao.vehicle.VehicleDAO;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
@Stateless
@LocalBean
public class VehicleLogic implements VehicleLogicRemote{
/**
* Default constructor.
*/
public VehicleLogic() {
// TODO Auto-generated constructor stub
}
@Override
public List<Vehicle> getVehicles(){
Context context = null;
VehicleDAOLocal dao = null;
try {
context = new InitialContext();
dao = (VehicleDAO)context.lookup("java:app/de.zielpuls.rent20.datamodel/VehicleDAO!de.zielpuls.rent20.server.dao.vehicle.VehicleDAO");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dao.getVehicles();
}
{code}
In the constructor of the DAO class, I want to instantiate my EntityManager.
Injection via @PersistenceContext also didn't work, the EntityManager was always null,but I think, this is the same problem.
{code}
package dao.vehicle;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
/**
* Session Bean implementation class VehicleDAO
*/
@Stateless
@LocalBean
public class VehicleDAO implements VehicleDAOLocal {
EntityManager em;
private EntityManagerFactory factory;
public VehicleDAO(){
factory = Persistence.createEntityManagerFactory("vehicle");
em = factory.createEntityManager();
}
}
{code}
In the constructor, the first line
factory = Persistence.createEntityManagerFactory("vehicle");
works (because I set <property name="hibernate.hbm2ddl.auto" value="create-drop" /> in the persistence.xml, even the tables are created, so this can't be a problem finding the MySQL driver), but in the second line
em = factory.createEntityManager();
I get at this stacktrace
{code}
ViewManagedReferenceFactory.getReference() line: 52 | ||
WritableServiceBasedNamingStore(ServiceBasedNamingStore).lookup(String, ServiceName) line: 133 | ||
WritableServiceBasedNamingStore(ServiceBasedNamingStore).lookup(Name) line: 74 | ||
NamingContext.lookup(Name) line: 178 | ||
InitialContext.lookup(Name) line: 123 | ||
InitialContext(NamingContext).lookup(String) line: 214 | ||
InitialContext.lookup(String) line: not available | ||
VehicleLogic.getVehicles() line: 58 | ||
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method] | ||
NativeMethodAccessorImpl.invoke(Object, Object[]) line: not available | ||
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: not available | ||
Method.invoke(Object, Object...) line: not available | ||
ManagedReferenceMethodInterceptorFactory$ManagedReferenceMethodInterceptor.processInvocation(InterceptorContext) line: 72 | ||
InterceptorContext.proceed() line: 288 | ||
WeavedInterceptor.processInvocation(InterceptorContext) line: 53 | ||
UserInterceptorFactory$1.processInvocation(InterceptorContext) line: 36 | ||
InterceptorContext.proceed() line: 288 | ||
SBInvocationInterceptor.processInvocation(InterceptorContext) line: 47 | ||
InterceptorContext.proceed() line: 288 | ||
InitialInterceptor.processInvocation(InterceptorContext) line: 21 | ||
InterceptorContext.proceed() line: 288 | ||
ChainedInterceptor.processInvocation(InterceptorContext) line: 61 | ||
ComponentDispatcherInterceptor.processInvocation(InterceptorContext) line: 53 | ||
InterceptorContext.proceed() line: 288 | ||
PooledInstanceInterceptor.processInvocation(InterceptorContext) line: 51 | ||
InterceptorContext.proceed() line: 288 | ||
CMTTxInterceptor.invokeInOurTx(InterceptorContext, TransactionManager, EJBComponent) line: 228 | ||
CMTTxInterceptor.required(InterceptorContext, EJBComponent, int) line: 304 | ||
CMTTxInterceptor.processInvocation(InterceptorContext) line: 190 | ||
InterceptorContext.proceed() line: 288 | ||
CurrentInvocationContextInterceptor.processInvocation(InterceptorContext) line: 41 | ||
InterceptorContext.proceed() line: 288 | ||
LoggingInterceptor.processInvocation(InterceptorContext) line: 59 | ||
InterceptorContext.proceed() line: 288 | ||
NamespaceContextInterceptor.processInvocation(InterceptorContext) line: 50 | ||
InterceptorContext.proceed() line: 288 | ||
AdditionalSetupInterceptor.processInvocation(InterceptorContext) line: 32 | ||
InterceptorContext.proceed() line: 288 | ||
TCCLInterceptor.processInvocation(InterceptorContext) line: 45 | ||
InterceptorContext.proceed() line: 288 | ||
ChainedInterceptor.processInvocation(InterceptorContext) line: 61 | ||
ViewService$View.invoke(InterceptorContext) line: 165 | ||
ViewDescription$1.processInvocation(InterceptorContext) line: 173 | ||
InterceptorContext.proceed() line: 288 | ||
ChainedInterceptor.processInvocation(InterceptorContext) line: 61 | ||
ProxyInvocationHandler.invoke(Object, Method, Object[]) line: 72 | ||
VehicleLogic$$$view4.getVehicles() line: not available | ||
VehicleService.getVehicles() line: 64 | ||
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method] | ||
NativeMethodAccessorImpl.invoke(Object, Object[]) line: not available | ||
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: not available | ||
Method.invoke(Object, Object...) line: not available | ||
MethodInjectorImpl.invoke(HttpRequest, HttpResponse, Object) line: 155 | ||
ResourceMethod.invokeOnTarget(HttpRequest, HttpResponse, Object) line: 257 | ||
ResourceMethod.invoke(HttpRequest, HttpResponse, Object) line: 222 | ||
ResourceLocator.invokeOnTargetObject(HttpRequest, HttpResponse, Object) line: 152 | ||
ResourceLocator.invoke(HttpRequest, HttpResponse) line: 91 | ||
SynchronousDispatcher.getResponse(HttpRequest, HttpResponse, ResourceInvoker) line: 525 | ||
SynchronousDispatcher.invoke(HttpRequest, HttpResponse, ResourceInvoker) line: 502 | ||
SynchronousDispatcher.invoke(HttpRequest, HttpResponse) line: 119 | ||
ServletContainerDispatcher.service(String, HttpServletRequest, HttpServletResponse, boolean) line: 208 | ||
HttpServletDispatcher.service(String, HttpServletRequest, HttpServletResponse) line: 55 | ||
HttpServletDispatcher.service(HttpServletRequest, HttpServletResponse) line: 50 | ||
HttpServletDispatcher(HttpServlet).service(ServletRequest, ServletResponse) line: 847 | ||
ApplicationFilterChain.internalDoFilter(ServletRequest, ServletResponse) line: 329 | ||
ApplicationFilterChain.doFilter(ServletRequest, ServletResponse) line: 248 | ||
StandardWrapperValve.invoke(Request, Response) line: 275 | ||
StandardContextValve.invoke(Request, Response) line: 161 | ||
WebNonTxEmCloserValve.invoke(Request, Response) line: 50 | ||
SecurityContextAssociationValve.invoke(Request, Response) line: 153 | ||
StandardHostValve.invoke(Request, Response) line: 155 | ||
ErrorReportValve.invoke(Request, Response) line: 102 | ||
StandardEngineValve.invoke(Request, Response) line: 109 | ||
CoyoteAdapter.service(Request, Response) line: 368 | ||
Http11Processor.process(Socket) line: 877 | ||
Http11Protocol$Http11ConnectionHandler.process(Socket) line: 671 | ||
JIoEndpoint$Worker.run() line: 930 | ||
Thread.run() line: not available {code} |
a NullPointerException (in ViewManagedReferenceFactory.getReference() line: 52) with detail message null and as cause only again a NullPointerException and there in again and again.
(The line were I call is the createEntityManager() method is "VehicleLogic.getVehicles() line: 58")
I also can see the exception only during debugging in the variables-view of Eclipse, nothing gets out to the console.
My persistence.xml looks like
{code: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="vehicle" transaction-type="JTA">
<!-- <provider>org.hibernate.ejb.HibernatePersistence</provider>-->
<jta-data-source>java:/MySqlDS</jta-data-source>
<class>de.zielpuls.rent20.server.datamodel.vehicle.Vehicle</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>
{code:xml}
and I added the driver via datasource as a modul:
{code:xml}
<datasource jta="false" jndi-name="java:/MySqlDS" pool-name="MySqlDS_Pool" enabled="true" use-ccm="false">
<connection-url>jdbc:mysql://localhost:3306/vehicle</connection-url>
<driver>com.mysql</driver>
<security>
<user-name>root</user-name>
<password>mysql</password>
</security>
</datasource>
<drivers>
<driver name="com.mysql" module="com.mysql">
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
{code:xml}
But as I said, tables are created, so I think it isn't a problem of the DB driver, and I also got the same exception with H2 database.
Now I don't know further anymore...
Maybe somebody has an idea?
Thank you and best regards
Markus