Empty EntityManager in Java EE bean on JBoss 5.1
I'm a newbie in Java EE. This is the first project I'm trying to do and I have a problem that I just can't solve.
I've created three projects: slowka-beans (EJB), slowka-persistance (JPA) and slowka-web(JSF). After deploying them I can't get access to persistence unit - the EntityManager is null开发者_运维技巧. Everything works fine - I can create beans, inside them instantiate entity classes and show them on JSF page. But how can I store them in the DB? I have MySQL database configured on JBoss site.
The code that I have looks following: LanguagesManager.java (in slowka-beans)
@Stateless
public class LanguagesManager implements LanguagesManagerLocal {
@PersistenceContext(unitName="slowka-persistance")
private EntityManager em;
public LanguagesManager() {
System.out.println("LanguagesManagerBean constructor");
}
public String getWorking() {
if(em == null) {
System.out.println("Not working...");
return "Not working...";
} else {
System.out.println("It's ALIVE!");
return "It's ALIVE!";
}
}
}
persistence.xml (slowka-persistance):
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="slowka-persistance">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/PraktykiDS</jta-data-source>
<class>pl.edu.uj.sobczak.szymon.Language</class>
</persistence-unit>
</persistence>
Deploying this on server doesn't cause any exceptions. but I've spotted the following warnings in server's output:
23:02:23,801 INFO [PersistenceUnitDeployment] Starting persistence unit persistence.unit:unitName=slowka.ear/slowka-persistance.jar#slowka-persistance
23:02:23,803 INFO [Ejb3Configuration] Processing PersistenceUnitInfo [
name: slowka-persistance
...]
23:02:23,804 WARN [Ejb3Configuration] Persistence provider caller does not implement the EJB3 spec correctly. PersistenceUnitInfo.getNewTempClassLoader() is null.
(... trimmed ...)
23:02:23,868 INFO [SessionFactoryObjectFactory] Bound factory to JNDI name: persistence.unit:unitName=slowka.ear/slowka-persistance.jar#slowka-persistance
23:02:23,868 WARN [SessionFactoryObjectFactory] InitialContext did not implement EventContext
Every time I'm accessing LanguagesManager::getWorking()
from JSP i'm getting "Not working..." output.
I've created the project in Eclipse, JPA is using EclipseLink. I've tried both - EclipseLink 1.1.4 and 2.1.0 with the same result.
Can please you help me?
Don't call EJBs from JSPs. Call them from servlets, where you inject them with @EJB
.
If you instantiate the object manually, then injection does not happen. The object (ejb) must be instantiated by the EJB container.
精彩评论