Reputation: 1203
I've read hundreds of answers on the same topic but I'm still unable to make it work! I call my EntityManager in this way:
@Stateless
public class MyTest {
@PersistenceContext
private EntityManager entityManager;
public Long create(MyUser user) {
entityManager.persist(user);
return user.getId();
}
}
Then I call this class by means of REST web services, in this way:
@Path("/myapp/{username}")
@ApplicationScoped
public class MyResource implements Serializable {
private static final long serialVersionUID = 1L;
MyTest service = new MyTest();
@POST
@Consumes({"application/json"})
@Produces({"text/plain"})
public String create(@Valid MyUser user) {
MyUser testUser = new MyUser();
//testUser.setParams(some_parameters)
return service.create(testUser).toString();
return "";
}
}
When I make a POST request I always get a NullPointerException, since the EntityManager is always null.
Below my persistence.xml, but I guess the problem is not there, since my app deploys well and perfectly creates the tables in the database.
<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="myserver-unit" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/MyData</jta-data-source>
<properties>
<property name="javax.persistence.schema-generation.create-source" value="metadata-then-script"/>
<property name="javax.persistence.schema-generation.drop-source" value="metadata-then-script"/>
<property name="javax.persistence.schema-generation.create-script-source" value="create-script.sql"/>
<property name="javax.persistence.schema-generation.drop-script-source" value="drop-script.sql"/>
<property name="javax.persistence.sql-load-script-source" value="load-script.sql"/>
<property name="eclipselink.logging.level" value="FINEST"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="eclipselink.jdbc.user" value="APP"/>
<property name="eclipselink.jdbc.password" value="APP"/>
</properties>
Please can you give me a hint? Is there an alternative way to initialize the EntityManager? I'm working with Netbeans 8 and GlassFish 4.
I already had some issues with injection, that's why I call the method in MyTest in the "standard way", instead of injecting it.
Upvotes: 0
Views: 270
Reputation: 1487
You can't instanciate a container managed object (like an EJB) that way. You have to inject your EJB by annotating it with @EJB. That is the only way unless you want to use manual JNDI lookup but there's no need to do that.
Also you probably should change @ApplicationScoped to @RequestScoped being a JAX-RS endpoint and all - or change MyTest from @Stateless to @Singleton otherwise you have a context mismatch between @ApplicationScoped and @Stateless
Upvotes: 1