Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top