title: java.lang.ClassCastException,Getting Entitymanager Via JNDI Lookup
body: <p>I am new to JPA and developing a webapp(J2EE) where the webapp is in Tomcat so I can't use @PersistenceContext. I decided to use a Helper class and everything was going fine. Then I decided to implement JNDI for connection pooling and I managed to get Datasource.</p>
  
  <p>The Helper Class looks like the following:</p>
  
  <pre><code>try {
      Context initCtx = new InitialContext();
      entityManager =                                   //class cast exception
          (EntityManager)initCtx.lookup(
              "java:/comp/env/jdbc/LCDS"
          );
      DataSource ds= (DataSource)initCtx.lookup(
              "java:/comp/env/jdbc/LCDS"
      ); 
      System.out.println(ds.getConnection()+"Cool");
      //jdbc:mysql://localhost:3306/XXXXXXX, UserName=root@localhost, MySQL-AB JDBC  DriverCool
      emf=(EntityManagerFactory) source.getConnection();      //class cast exception
      emf = Persistence.createEntityManagerFactory("XXXX");   //working version
  } 
  </code></pre>
  
  <p>The error is:    </p>
  
  <pre><code>ava.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to javax.persistence.EntityManager
  </code></pre>
  
  <p>I don't know where I am getting wrong. I am not able to get EntityManagerFactory or EntityManager via JNDI lookup. I tried @Resource(name="jdbc/LCDS") and @PersistenceUnit(name="jdbc/LCDS").</p>