意外的 ClassCastException...使用 EJB
我面临一个非常小的问题,这让我发疯。
我正在从名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)