lundi 29 juin 2015

EJB java.lang.ClassCastException

I'm using Eclipse with Glassfish for my first steps in Java EE. I've created three eclipse projects (JPA project, EJB project Web project). I've created a local EJB bean TestBean with a local interface TestBeanLocal:

@Stateless
@LocalBean
class TestBean implements TestBeanLocal {
  @Override
  public void doSomething(List<JPAEntity> myEntities) {
    for(JPAEntity a : myEntities) {

    }
  }
}

and a ManagedBean that uses the EJB:

@MangagedBean
public class MyBean {
    @EJB
    private TestBeanLocal testBean;

    @PostConstruct
    private void init() {
       //load JPAEntity from Database
       List<JPAEntity> myEntities = ....
       testBean.doSomething(myEntities);
    }
}

My problem is that I get a ClassCastException at the for loop in the TestBean.

java.lang.ClassCastException: us.mypackage.jpa.JPAEntity cannot be cast to us.mypackage.jpa.JPAEntity

I found another stackoverflow question that says that this error message is because of two different classloaders. How can I fix this? Can I tell EJB to use the same classloader that my webproject uses?

Aucun commentaire:

Enregistrer un commentaire