意外的 ClassCastException...使用 EJB

发布于 2024-12-29 05:14:46 字数 1467 浏览 1 评论 0原文

我面临一个非常小的问题,这让我发疯。

我正在从名为 SlBrowseProducts 的无状态会话bean 调用业务方法“public List getPsubcategoryList()”。该方法使用实体管理器实例从映射的数据库表中检索对象列表名为 Psubcategory 的实体类。这是我使用namedQuery 完成的。返回的列表包含 Object 类型的元素,我必须附加一个名为 myIterator 的迭代器来列出并将每个 myIterator.next() 转换为 Psubcategory 类型,以使结果元素有用并在其上运行任何 getter。然而,这个转换返回了以下错误..

SEVERE: java.lang.ClassCastException:entitybeans.Psubcategory无法转换为entitybeans.Psubcategory

我尝试广泛搜索以找到它的原因..我来到了跨越可能导致它的“类加载器地狱”的东西。

这个问题有什么解决方案吗?还有其他方法可以解决这个问题吗?我尝试过不使用迭代器,而是使用 for 循环……但即便如此,我也必须进行强制转换……这又让我遇到了同样的问题……

请帮助我严格按计划进行。

====================JSP页面

List<Psubcategory> subcategoryList1 = slbp.getProductSubcategories();                
if(subcategoryList1.size()!=0){
   for(int i=0;i<subcategoryList1.size();i++){
      Psubcategory temp = subcategoryList1.get(i);
      System.out.print(temp.getSubcategory());
   }
}

============================ ============SLBrowseProducts.java======================

@Stateless
@LocalBean
public class SLBrowseProducts implements TestInterface {

   EntityManagerFactory emf = Persistence.createEntityManagerFactory("INNOVATIVE-INDOORSPU");
   EntityManager em = emf.createEntityManager();    

   @Override
   public List getProductSubcategories(){
      List subcategoryList=em.createNamedQuery("Psubcategory.findAll").getResultList();        
      return subcategoryList;
   }
}

I am facing a very small problem which is driving me mad..

I am calling a business method 'public List getPsubcategoryList()' from a stateless sessionbean named SlBrowseProducts .. this method used entitymanager instance to retrieve a list of Objects from Database Table mapped to Entity class named Psubcategory. this I did using a namedQuery. The list returned contains elements of type Object, I have to attach a iterator named myIterator to list and cast eachof the myIterator.next()'s to the type Psubcategory to make the result element useful and run any getters on it. However this casting returned the following error..

SEVERE: java.lang.ClassCastException: entitybeans.Psubcategory cannot be cast to entitybeans.Psubcategory

I tried googling extensively to find the cause for it.. to which I came across something called 'ClassLoader Hell' which might be causing it..

Is there any solution to this problem. Is there any other way around this... I have tried not using Iterator and instead using for loop ... but even then I have to cast.. which again lands me to the same problem..

Please Help m tight on schedule.

====================JSP PAGE==================

List<Psubcategory> subcategoryList1 = slbp.getProductSubcategories();                
if(subcategoryList1.size()!=0){
   for(int i=0;i<subcategoryList1.size();i++){
      Psubcategory temp = subcategoryList1.get(i);
      System.out.print(temp.getSubcategory());
   }
}

======================SLBrowseProducts.java======================

@Stateless
@LocalBean
public class SLBrowseProducts implements TestInterface {

   EntityManagerFactory emf = Persistence.createEntityManagerFactory("INNOVATIVE-INDOORSPU");
   EntityManager em = emf.createEntityManager();    

   @Override
   public List getProductSubcategories(){
      List subcategoryList=em.createNamedQuery("Psubcategory.findAll").getResultList();        
      return subcategoryList;
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七秒鱼° 2025-01-05 05:14:46
@Stateless
@LocalBean
public class SLBrowseProducts implements TestInterface {

   EntityManagerFactory emf = Persistence.createEntityManagerFactory("INNOVATIVE-INDOORSPU");
   EntityManager em = emf.createEntityManager();    

   @Override
   public List<Psubcategory> getProductSubcategories(){
         List<Psubcategory> subcategoryList=
                                em.createNamedQuery("Psubcategory.findAll").getResultList();        
      return subcategoryList;
   }
}
============================================================================================
List<Psubcategory> subcategoryList1 = slbp.getProductSubcategories();                
   for(Psubcategory temp : subcategoryList1){
      System.out.print(temp.getSubcategory());
   }
@Stateless
@LocalBean
public class SLBrowseProducts implements TestInterface {

   EntityManagerFactory emf = Persistence.createEntityManagerFactory("INNOVATIVE-INDOORSPU");
   EntityManager em = emf.createEntityManager();    

   @Override
   public List<Psubcategory> getProductSubcategories(){
         List<Psubcategory> subcategoryList=
                                em.createNamedQuery("Psubcategory.findAll").getResultList();        
      return subcategoryList;
   }
}
============================================================================================
List<Psubcategory> subcategoryList1 = slbp.getProductSubcategories();                
   for(Psubcategory temp : subcategoryList1){
      System.out.print(temp.getSubcategory());
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文