将数据传递到 EJB 模块时出现 ClassCastException

发布于 2024-10-10 07:35:51 字数 2671 浏览 3 评论 0原文

我有一个使用 JSF & 完成的 Web 项目我也有一个 EJB 模块。 我在 FORM & 中输入一些数据尝试通过我的会话 bean 将这些数据保存到数据库。

步骤顺序如下。

1)用户打开带有表单的屏幕
2)他用数据填写表单字段
3)点击保存按钮
4) 托管 bean 捕获所有输入的值&将它们添加到 ArrayList(包含 Entity beans。如下所示)

for(String tagName : formTagNames){ // For all the tag names in the FORM
 String value = request.getParameter(tagName); // Get the value of the field
 myArrayList.add(new DynamicForm(tagName, value)); // Create a bean & add to list
}

5) 然后将 myArrayList 发送到 EJB 模块进行持久化。 (我也正确地完成了映射)

@EJB(name="DynamicFormFacadeLocal", mappedName="DynamicFormFacade/local")
private static DynamicFormFacadeLocal dynamicFormFacadeLocal;  

// Send it to EJB module for persistence
dynamicFormFacadeLocal.addDynamicFormFields(myArrayList);  

6)当我到达会话 bean 时尝试迭代 myArrayList

public Integer addDynamicFormFields(ArrayList<DynamicForm> formDetailsList) {
 if(formDetailsList == null || formDetailsList.isEmpty()) return 0;

 setupResources(); // Setting up all necessary connections & stuff
 int count = 0;
 try{
     entityTransaction.begin();
     for(DynamicForm form : formDetailsList){
  entityManager.persist(form);
  count++;
     }
     entityTransaction.commit();
 }
 catch(Exception e){
     e.printStackTrace();
     entityTransaction.rollback();
 }
 finally{
     cleanupResources(); // Cleanup all connections & resources
 }

 return count;
    }

7) 但它给了我一个 ClassCastException &我不明白为什么..!! :(

18:17:42,210 INFO  [org.hibernate.impl.SessionFactoryImpl] building session factory
18:17:42,214 INFO  [org.hibernate.impl.SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured
18:17:42,219 ERROR [STDERR] java.lang.ClassCastException: com.test.dynamiform.beans.entity.DynamicForm cannot be cast to com.test.dynamiform.beans.entity.DynamicForm
18:17:42,219 ERROR [STDERR]  at com.test.dynamiform.beans.session.DynamicFormFacade.addDynamicFormFields(DynamicFormFacade.java:32)
18:17:42,219 ERROR [STDERR]  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
18:17:42,219 ERROR [STDERR]  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
18:17:42,219 ERROR [STDERR]  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
18:17:42,219 ERROR [STDERR]  at java.lang.reflect.Method.invoke(Unknown Source)
18:17:42,220 ERROR [STDERR]  at org.jboss.aop.joinpoint.MethodInvocation.invokeTarget(MethodInvocation.java:122)
.... etc

有人可以帮我解决这个问题吗..???
当我发送邮件时我完全迷失了方向迭代相同类型的对象...:(

提前致谢。
阿塞拉。

I have a web project which is done using JSF & I have an EJB module too.
I enter some data in a FORM & try to save those data to DB via my session bean.

The sequence of steps is as follows.

1) User opens the screen with a form
2) He fills the form fields with data
3) Hits on the save button
4) The managed bean captures all the entered values & adds them to an ArrayList(containing Entity beans. shown below)

for(String tagName : formTagNames){ // For all the tag names in the FORM
 String value = request.getParameter(tagName); // Get the value of the field
 myArrayList.add(new DynamicForm(tagName, value)); // Create a bean & add to list
}

5) Then myArrayList is sent to the EJB module for persistence. (I have done the mapping correctly too)

@EJB(name="DynamicFormFacadeLocal", mappedName="DynamicFormFacade/local")
private static DynamicFormFacadeLocal dynamicFormFacadeLocal;  

// Send it to EJB module for persistence
dynamicFormFacadeLocal.addDynamicFormFields(myArrayList);  

6) When I reach the session bean & try to iterate through myArrayList

public Integer addDynamicFormFields(ArrayList<DynamicForm> formDetailsList) {
 if(formDetailsList == null || formDetailsList.isEmpty()) return 0;

 setupResources(); // Setting up all necessary connections & stuff
 int count = 0;
 try{
     entityTransaction.begin();
     for(DynamicForm form : formDetailsList){
  entityManager.persist(form);
  count++;
     }
     entityTransaction.commit();
 }
 catch(Exception e){
     e.printStackTrace();
     entityTransaction.rollback();
 }
 finally{
     cleanupResources(); // Cleanup all connections & resources
 }

 return count;
    }

7) But it gives me a ClassCastException & I don't understand why..!! :(

18:17:42,210 INFO  [org.hibernate.impl.SessionFactoryImpl] building session factory
18:17:42,214 INFO  [org.hibernate.impl.SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured
18:17:42,219 ERROR [STDERR] java.lang.ClassCastException: com.test.dynamiform.beans.entity.DynamicForm cannot be cast to com.test.dynamiform.beans.entity.DynamicForm
18:17:42,219 ERROR [STDERR]  at com.test.dynamiform.beans.session.DynamicFormFacade.addDynamicFormFields(DynamicFormFacade.java:32)
18:17:42,219 ERROR [STDERR]  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
18:17:42,219 ERROR [STDERR]  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
18:17:42,219 ERROR [STDERR]  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
18:17:42,219 ERROR [STDERR]  at java.lang.reflect.Method.invoke(Unknown Source)
18:17:42,220 ERROR [STDERR]  at org.jboss.aop.joinpoint.MethodInvocation.invokeTarget(MethodInvocation.java:122)
.... etc

Can someone please help me on this one..???
I'm totally lost here as I'm sending & iterating the same TYPE of objects... :(

Thanks in advance.
Asela.

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

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

发布评论

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

评论(1

还如梦归 2024-10-17 07:35:51

您正处于所谓的“类加载器地狱”中。

对象的类型取决于类以及加载它的类加载器。

您将在服务器中部署该类的两份副本,一份可能在 Web 应用程序中,一份在 EJB 中,并且不同的代码位从不同的类加载器中获取该类。

从 Web 应用程序中删除该类,进行安排,以便您的 Web 应用程序和 EJB 从同一位置加载共享类 - 通常在 EJB 中将该类放在一起是最好的选择。

然而,我不喜欢 EJB“了解”Form 对象,使用简单的 DTO 类可能会更干净,尽管相当烦人。

You're in what has been termed "Classloader Hell".

The type of an object depends upon the Class and also the classloader which loaded it.

You will have two copies of that class deployed in your server, perhaps one in the Web App and one in the EJB and different bits of code are picking up the class from different classloader.

Remove the one from the Web App, arrange things so that your Web App and EJB load the shaed class from the same place - often having the class in the EJB is the best bet.

However, I don't like EJBs "knowing" about Form objects, using a simple DTO class may be cleaner if rather annoying.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文