• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JNDI look for EntityManager

 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to JNDI lookup for EntityManager? How?

In ejb 3 in action, page-432,

-----------
return (EntityManager)ctx.lookup(EM_NAME);
----------

but in my code also i am doing like this-\

CarEAOImpl.java
----------------------
package Factory;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.sql.DataSource;

import Pack.Car;

public class CarEAOImpl implements CarEAO{


public CarEAOImpl(){

}

private EntityManager getEntityManager() throws NamingException{
InitialContext ctx = null;
try{
ctx = new InitialContext();

}
catch(Exception e){

}
return (EntityManager)ctx.lookup("jdbc/ActionBazaarDS");
}
public void addCar(String steering, String horn, String wheel) {
EntityManager em;
try {
em = getEntityManager();
Car c = new Car(steering, horn, wheel);
em.persist(c);

} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
---------------------------------------

when it is doing lookup for DataSource, it is giving exception. As the returned value is DataSource and it cannot cast to EntityManager.

But how it was done in book.

Moreover, in Glassfish, we can set only JDBC datasources only, is it possible to set EntityManager also, so that directly i can look for it.
 
jeff mutonho
Ranch Hand
Posts: 271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amandeep
Look at the example at Sahoo's java.net blog.He talks about 2 approaches on how to use JNDI lookup to get your EM.
The article is at Sahoo's Blog

If you're lazy to read the whole article , just look under the sub-heading "Container Managed Entity Manager"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic