java.lang.ClassCastException,通过JNDI查找获取Entitymanager [英] java.lang.ClassCastException,Getting Entitymanager Via JNDI Lookup

查看:127
本文介绍了java.lang.ClassCastException,通过JNDI查找获取Entitymanager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JPA的新手,正在开发一个webapp(J2EE),其中的webapp位于Tomcat中,因此我不能使用@PersistenceContext.我决定使用Helper类,一切正常.然后,我决定为连接池实现JNDI,并设法获得了数据源.

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.

Helper类如下:

The Helper Class looks like the following:

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
} 

错误是:

ava.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to javax.persistence.EntityManager

我不知道哪里出了问题.我无法通过JNDI查找获得EntityManagerFactory或EntityManager.我尝试了@Resource(name ="jdbc/LCDS")和@PersistenceUnit(name ="jdbc/LCDS").

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").

推荐答案

更新
由于Tomcat的限制,无法对持久性单元进行JNDI访问.请参见 JPA Tomcat限制.您将必须使用emf = Persistence.createEntityManagerFactory("UNIT NAME").
对不起,造成误导.我已经在WebSphere Liberty上进行了测试,但手头没有Tomcat.

UPDATE
JNDI access to persistence unit is not possible due to Tomcat limitations. See JPA Tomcat limitations. You will have to use emf = Persistence.createEntityManagerFactory("UNIT NAME").
Sorry for misleading answer. I've tested that on WebSphere Liberty, didn't have Tomcat at hand.

如果需要该功能,请检查

If you need that functionality check WebSphere Liberty, which is as fast and lightweight as Tomcat, but is fully Java EE Web profile compliant. It has lots of useful features like JPA, EJBLite, JAX-RS already available if needed, without fighting with additional libraries configuration.

更新结束

我已经检查过WebSphere Liberty,您需要创建参考来通过JNDI查找持久性单元.您可以通过以下两种方法进行创建:

I've checked on WebSphere Liberty, you need to create reference to lookup your persistence unit via JNDI. You have two options to create that:

  • 在类级别使用注释
    在您的任何servlet中,都需要使用以下代码定义注释:
  • Use annotation at the class level
    In any of your servlets you need to define annotation using the follownig:

@PersistenceUnit(name="JPATestRef", unitName="UnitName")
public class JPATester extends HttpServlet {
...

@PersistenceUnit(name="JPATestRef", unitName="UnitName")
public class JPATester extends HttpServlet {
...

  • 使用web.xml
  • 中的条目

    • Use entry in web.xml
    • <persistence-unit-ref>
        <persistence-unit-ref-name>JPATestRef</persistence-unit-ref-name>
        <persistence-unit-name>UnitName</persistence-unit-name>
      </persistence-unit-ref>
      

      然后使用以下代码对其进行访问:

      Then you access it using the following code:

      try {
          InitialContext ctx = new InitialContext();
          System.out.println("looking EntityManagerFactory:");
          EntityManagerFactory emf2 = (EntityManagerFactory) ctx.lookup("java:comp/env/JPATestRef");
          System.out.println("emf:2" + emf2);
      
      } catch (NamingException e) {
      

      这篇关于java.lang.ClassCastException,通过JNDI查找获取Entitymanager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆