Reputation: 377
My project is running on Glassfish 3.0.1 I'm trying to use @PersistenceContext annotation to inject EntityManager. Here is my code:
@Stateless
public class UserBean implements UserBeanRemote {
@PersistenceContext(unitName = "RHDManagementPlatformPU")
private EntityManager em;
public UserBean() {
if (this.em == null) {
System.err.println("NULL");
}
}
...
}
In console I get error message "NULL". I already saw 5-6 topics which are similar to mine. But the only thing I understood is maybe the problem is in my persistence.xml. Here is it:
<?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="RHDManagementPlatformPU" transaction-type="JTA">
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
<jta-data-source>dev_magi_entities</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="toplink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
I'tried to find on the internet how to configure this file, but did not find where the issue could be. Maybe someone have any idea or tutorial, manual links which can help me to better understand how this beast works =)
Upvotes: 1
Views: 1609
Reputation: 16273
The resource injection doesn't happen during bean creation, but when the bean is injected by the container. In other words, the resource is injected after the bean constructor is invocated.
You need to perform your test in a method annotated with @PostConstruct
:
@PostConstruct
public void init() {
if (this.em == null) {
System.err.println("NULL");
}
}
As written in the linked JavaDoc:
The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done
EDIT based on the comment below.
If you are by any chance trying to instantiate the bean yourself, by means of its constructor, the injection will fail. The ways to use Enterprise Java Beans (like a @Stateless
bean) are to inject them by means of @EJB
annotation, or to use JNDI lookup. Pay attention that you must inject the bean in another container-managed object (like a JSF bean or a CDI bean for example).
See this link for further reference.
Upvotes: 4
Reputation: 1335
I Recently had the same problem.
We needed create JNDI ref to the jdbc resource.
Here my additional configuration.
glassfish-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD
GlassFish Application Server 3.1 Servlet 3.0//EN"
"http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
<session-config>
<session-manager />
</session-config>
<resource-ref>
<res-ref-name>java:app/env/mysql</res-ref-name>
<jndi-name>java:jdbc/mysql</jndi-name>
<!-- My JDBC Resource -->
</resource-ref>
<jsp-config />
</glassfish-web-app>
And my web.xml :
<data-source>
<name>java:app/env/mysql</name>
<class-name>com.mysql.jdbc.jdbc2.optional.MysqlDataSource</class-name>
<database-name>psi</database-name>
<user>user</user>
<password>pass</password>
</data-source>
Upvotes: 0