Samstag, Juni 6, 2020

java.lang.Classcastexception-Fehler: com.Sonne.proxy.$Proxy geworfen wird, wenn ein EJB-casting

Ich bin, die Probleme mit remote-EJB-Objekte. JNDI-look-up ist erfolgreich, aber die Umwandlung in ein Objekt und die spätere Verwendung fehl. Die EJB-Schnittstelle und die Umsetzung sind wie folgt:

package books.pointejb;

import java.util.List;

import javax.ejb.Remote;

import books.pointejb.User;
import books.pointejb.Book;

public interface UserViewBeanRemote {
    public boolean register(User user);
    //A user can delete his/her account, note that two users with the same username cannot exist
    public void delete(User user);
    public boolean login(User user);
    public boolean logout(User user);
    //Search only by book titles
    public List<Book> search(String title);

package books.pointejb;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.ejb.Stateful;
import javax.persistence.EntityManager;  
import javax.persistence.PersistenceContext;

//import javax.ejb.Remote;
import books.pointejb.User;
import books.pointejb.Book;

 * Session Bean implementation class UserView
public class UserViewBean implements UserViewBeanRemote {

     private EntityManager entityManager; 
     * Default constructor. 
    public UserViewBean() {
        //TODO Auto-generated constructor stub

    public static <T> List<T> castList(Class<? extends T> clazz, Collection<?> c) {
        List<T> r = new ArrayList<T>(c.size());
        for(Object o: c)
        return r;

    public boolean register(User user) {
        //We have servlets that validate these fields, so we don't validate anything here
        //We check to see whether there is already a user with the same user name or not
        Query query = (Query) entityManager.createQuery("SELECT u FROM users u WHERE u.username=:userName");
        ((javax.persistence.Query) query).setParameter("userName", user.getUsername());
        //We check each returned value. If we find anything than we do not add the user
        List<User> users = castList(User.class, ((javax.persistence.Query) query).getResultList());
        if(users.isEmpty()) {
            //The user is not present. Add this user
            //return "Welcome to BooksPoint " + user.getUsername() + "!";
            return true;
        } else {
            //return "Cannot create " + user.getUsername() + ". A user with this name already exists";
            return false;


    public void delete(User user) {
        //The account automatically exists since the user is already logged in
        Query query = (Query) entityManager.createQuery("DELETE FROM users u WHERE u.username=:userName");
        ((javax.persistence.Query) query).setParameter("userName", user.getUsername());
        //return "Your account " + user.getUsername() + " has been removed successfully!";

    public boolean login(User user) {
        //Check if we have a valid user/pass pair
        Query query = (Query) entityManager.createQuery("SELECT u FROM users u WHERE u.username=:userName AND u.password=:password");
        ((javax.persistence.Query) query).setParameter("userName", user.getUsername());
        ((javax.persistence.Query) query).setParameter("password", user.getPassword());
        List<User> users = castList(User.class, ((javax.persistence.Query) query).getResultList());
        if(!users.isEmpty()) {
            //return "Welcome " + user.getUsername() + "!";
            return true;
        } else {
            //return "Username or password are not valid";
            return false;

    public boolean logout(User user) {
        //This can be done in the jsp/servlet. Simply erase the session variables associated with the current user
        return false;

    public List<Book> search(String title) {
        //Check all entries for names similar to title
        Query query = (Query) entityManager.createQuery("SELECT * FROM books WHERE title LIKE '%:title%'");
        ((javax.persistence.Query) query).setParameter("title", title);
        List<Book> books = castList(Book.class, ((javax.persistence.Query) query).getResultList());
        //Return the books now leave the rest to the jsp
        return books;

Einkommentieren @Remote(UserViewBeanRemote.class) nicht, dieses Verhalten zu ändern.
Der lookup erfolgt in der folgenden Datei:

package books.point;

import javax.naming.Context;
import javax.naming.NamingException;

import books.point.clientutility.ClientUtility;
import books.pointejb.CartBean;
import books.pointejb.CartBeanRemote;
import books.pointejb.UserViewBean;
import books.pointejb.UserViewBeanRemote;

public class Lookup {
    private static final String MODULE_NAME = "BooksPointEJB";

    public static UserViewBean doLookupUser() {
        Context context = null;
        UserViewBean bean = null;
        try {
            //1. Obtaining Context
            context = ClientUtility.getInitialContext();
            //2. Generate JNDI Lookup name
            String lookupName = getLookupNameUser();
            //3. Lookup and cast
            bean = (UserViewBean) context.lookup(lookupName); //<== Exception is thrown here

        } catch (NamingException e) {
        return bean;

    public static String getLookupNameUser() {
         * The app name is the EAR name of the deployed EJB without .ear suffix.
         * Since we haven't deployed the application as a .ear, the app name for
         * us will be an empty string
        String appName = "";

         * The module name is the JAR name of the deployed EJB without the .jar
         * suffix.
        String moduleName = MODULE_NAME;

         * AS7 allows each deployment to have an (optional) distinct name. This
         * can be an empty string if distinct name is not specified.
        String distinctName = "";

        //The EJB bean implementation class name
        String beanName = UserViewBean.class.getSimpleName();

        //Fully qualified remote interface name
        final String interfaceName = UserViewBeanRemote.class.getName();

        //Create a look up string name
        //Be very careful about the stateful flag at the end
        String name = "ejb:" + appName + "/" + moduleName + "/" + distinctName
                + "/" + beanName + "!" + interfaceName + "?stateful";

//Benutzer nachgeschlagen name: ejb:/BooksPointEJB//UserViewBean!Bücher.pointejb.UserViewBeanRemote?stateful
System.aus.println(„User-Lookup-name:“ + name);
return name;


Den EJB-deployment-log steht:

12:10:22,897 INFO  [] (MSC service thread 1-5) JNDI bindings for session bean named UserViewBean in deployment unit deployment "BooksPoint.war" are as follows:


Ich bin mit jboss-as-7.1.1.Endgültig. Warum ist diese Ausnahme wird geworfen?

12:10:52,973 INFO  [org.jboss.ejb.client] (http-localhost- JBoss EJB Client version 1.0.5.Final
12:10:53,043 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/BooksPoint].[books.point.Register]] (http-localhost- Servlet.service() for servlet books.point.Register threw exception: java.lang.ClassCastException: com.sun.proxy.$Proxy22 cannot be cast to books.pointejb.UserViewBean
    at books.point.Lookup.doLookupUser( [classes:]
    at books.point.Register.doPost( [classes:]
    at javax.servlet.http.HttpServlet.service( [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at javax.servlet.http.HttpServlet.service( [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter( [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter( [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardWrapperValve.invoke( [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContextValve.invoke( [jbossweb-7.0.13.Final.jar:]
    at [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
    at [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.StandardHostValve.invoke( [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke( [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invoke( [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.service( [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Processor.process( [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process( [jbossweb-7.0.13.Final.jar:]
    at$ [jbossweb-7.0.13.Final.jar:]
    at Source) [rt.jar:1.7.0_45]

InformationsquelleAutor Sebi | 2014-02-09

1 Kommentar

  1. 4

    Wenn Sie eine JNDI-Suche durch, die Sie lookup der remote-Schnittstelle.

    so ändern Sie Ihre Stimmen, um

    bean = (UserViewBeanRemote) context.lookup(lookupName);

    sollte den trick tun.

    Das war das problem in der Tat. +1

    InformationsquelleAutor Max Fichtelmann

Kostenlose Online-Tests