on 07-27-2009 2:50 PM
Hi All,
I am working on a simple Java EE Project.
I want to access JPA entities in my DAO class which is a plain java class. Later I will call the DAO class from a Stateless session bean(business logic).
Now, the problem is that I am not able to access the JPA Persistence Context from my DAO class. I tried with the following code:
EntityManagerFactory emf = Persistence
* .createEntityManagerFactory("PS_LTP_PU");*
* EntityManager entityManager = emf.createEntityManager();*
I also
@PersistenceContext(unitName="PS_LTP_PU")
EntityManager entityManager;
In first case it is showing following exception:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named PS_LTP_PU
In second case it is throwing Null Pointer Exception for Entity Manager.
What is the problem?
How can I resolve this issue.
Thanks
Sampath
I resolved the problem.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Sampath,
your first atempt fails as the SAP JPA persistence provider is not visible to the application (nor to javax.persistence.Persistence).
Your second atempt fails because dependency injection is not available for plain java classes.
To obtain en entity manager from within a plain Java class, you should firstly bind it to the JNDI. The easiest way to achieve this is using the @PersistenceContext annotation put on you session bean:
@PersistenceContext(name="theJndiName", unitName="PS_LTP_PU")
@Stateless
public class YourBean {
...
Then, you can look it up in JNDI in your DAO
public class YourDao {
void someMethod() {
Context ctx = new InitialContext();
EntityManager em = ctx.lookup("theJndiName");
-Adrian
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Adrian,
Thanks for your reply.
I will try it and ask you if have any problem.
But one dougt is that, I want to abstract all persistence related thing from my business class(statless session bean) by putting it in plain DAO class.
But in your approach, Business session bean will aware of the persistence. Is it standered approach or any other approach is available for my case.
Thanks
Sampath
>But one dougt is that, I want to abstract all persistence related thing from my business class(statless session bean) by putting it in plain DAO class. But in your approach, Business session bean will aware of the persistence. Is it standered approach or any other approach is available for my case.
The thing is a Dependency Injection feature does not work in plain Java classes, only in EJB. Because only EJB Container is responsible for the injecting.
So you can try to create two EJB: one will be responsible for the persistence and another one will be responsible for your business logic.
BR, Sergei
Hi Sampath,
hm, you don't like @PersistenceContext on your stateless session bean beause it makes it "aware" of JPA.
Alternatively you can bind the persistence context into the JNDI by declaring it in the web.xml or ejb-jar.xml:
<persistence-context-ref>
<persistence-context-ref-name>theJndiName</persistence-context-ref-name>
<persistence-unit-name>PS_LTP_PU</persistence-unit-name>
</persistence-context-ref>
-Adrian
User | Count |
---|---|
83 | |
23 | |
11 | |
9 | |
8 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.