Empty EntityManager in Java EE bean on JBoss 5.1

I am new to Java EE. This is the first project I'm trying to do, and I have a problem that I just can’t solve.

I created three projects: slowka-beans (EJB), slowka-persistance (JPA) and slowka-web (JSF). After deploying them, I cannot access the continuity unit - EntityManager is NULL. Everything works fine - I can create beans, create instances of entity classes inside them and show them on the JSF page. But how can I store them in the database? I have a MySQL database configured on a JBoss site.

The code I have is as follows: 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 ALIVE!"); return "It 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 the server does not raise any exceptions. but I noticed the following warnings in the server 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 

Each time I access LanguagesManager::getWorking() from the JSP, I get the output "Doesn't work ...".

I created a project in Eclipse, JPA uses EclipseLink. I tried both - EclipseLink 1.1.4 and 2.1.0 with the same result.

Can you help me?

+1
source share
1 answer

Do not invoke the EJB from the JSP. Call them from the servlet where you enter them @EJB .

If you create an instance of the object manually, then the injection does not occur. An object (ejb) must be created by an instance of an EJB container.

+2
source

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


All Articles