-
1. Re: @PersistenceContext is null
saltnlight5 Jul 25, 2010 10:25 AM (in response to creativechips)1 of 1 people found this helpfulThe persistence's trancaction-type should be either "RESOURCE_LOCAL" or "JTA".
To be able to inject container's EM in your EJB as you described:
@PersistenceContext(unitName="MyClass") protected EntityManager em;
You would need to configure JTA in your persistence.xml.
<persistence-unit name="MyClass" transaction-type="JTA"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:/MyClassDatasource</jta-data-source> ...
If you still want to use RESOURCE_LOCAL type, I think you can still inject it, using EM Factory in your EJB, like this:
@PersistenceUnit(unitName="MyClass") private EntityManagerFactory factory;
But then your container will not able to manage your EM (persistence context)! Use JTA is more proper way in a EJB container in normal cases.
-
2. Re: @PersistenceContext is null
creativechips Jul 25, 2010 12:54 PM (in response to saltnlight5)Thank you for your quick reply Zemian,
Unfortunatly that didnt do the trick ... I applied the change as you described, but the EntityManager is still null.
Can you think of something else? Id appriciate anything. Thank you!
My console log is attached.
.
-
console.txt.zip 4.5 KB
-
-
3. Re: @PersistenceContext is null
saltnlight5 Jul 25, 2010 2:00 PM (in response to creativechips)1 of 1 people found this helpfulpersistence.xml defined in WAR is only accessable to EJBs defined within that scope. Are you having trouble with EJBs in WAR or in your separated EJB jar?
If deploying EJBs jar and WAR together that need to use persistence unit, try to package persistence unit jar in a EAR's lib properly, that way, both WAR and EJBs will have access to it.
Try to read upon proper EJB/WAR/EAR packaging and deployment.
-
4. Re: @PersistenceContext is null
creativechips Jul 28, 2010 1:11 AM (in response to saltnlight5)Thank you and sorry for the delay, I read around about deployment and packaging.
I switched to EAR deployment, it deploy fine (no exceptions and my code is running), but the PersistenceContext is still null...
My /deploy dir looks like this:
Ent.ear
| App.jar
| | com
| | | ... (my packages hierarchy)
| | META-INF
| | |-----persistence.xml
| Http.war
| | ... (my servlets, jsp`s and other js/css/...)
| META-INF
| |----application.xml
|----mysql-connector-java-5.1.10-bin.jar
My EJB (a @Stateless SB, which implement @Local and @Remote interfaces) is located in the App.jar.
Only access to the entityManager is done by that SB (for now), i.e. from the jar. This is where I get the null pointer.
-
5. Re: @PersistenceContext is null
jaikiran Jul 28, 2010 2:20 AM (in response to creativechips)How do you get hold of the EJB in your code? Are you sure you are doing a JNDI lookup? Can you post your client code where this lookup is happening? And also post the code where you are checking the injected persistence context.
-
6. Re: @PersistenceContext is null
creativechips Jul 30, 2010 7:09 AM (in response to jaikiran)Thank you Jaikiran! You got it. It was the JNDI call.
Fix was to call something like
(StatelessSB) getInitialContext()*.lookup("<EAR name>/statelessSB/local");
where statelessSB is set on name attribute of the Stateless annotation, on the SLSB class StatelessSB .
* getInitialContext() return a new javax.naming.InitialContext()
Zemian, many thanks as well of course.