users
[Top] [All Lists]

ClassCast Exception while trying to reference a local home object.

To: Cinjug <users@xxxxxxxxxx>
Subject: ClassCast Exception while trying to reference a local home object.
From: John Olmstead <jolmstead2k@xxxxxxxxx>
Date: Thu, 29 Apr 2004 12:34:01 -0700 (PDT)
Delivered-to: mailing list users@cinjug.org
Mailing-list: contact users-help@cinjug.org; run by ezmlm
Ladies and Gentlemen;
 
I have an entity bean that is locally referenced by stateless session bean.  After getting the context to the bean, I try to get a reference  to the home interface and this results in a ClassCast exception.  Relevent code is below.  Anybody have any ideas on what is happening here or how to debug this??
 
Many Thanks;
 
John Olmstead
DBAdirect
 
Here is the relevent code : 
 
Session Bean that is client of the local referenced Entity Bean:
 
package dbadirect.administration.timekeeping;
import javax.ejb.SessionBean;
import javax.ejb.EJBException;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import dbadirect.administration.helper.TimeAccountBean;

public class TimeAccountManageBean  implements SessionBean
{
    public SessionContext sessionContext;
    public TimeAccountLocal timeAccount = null;
    public Integer timeAccountPK = null;
    public void ejbActivate(){}
    public void ejbPassivate(){}
    public void ejbRemove(){}
    public void setSessionContext(SessionContext sc)
    {
           this.sessionContext = sc;
    }
    public SessionContext getSessionContext()
    {
           return sessionContext;
    }
    public void ejbCreate(){}
    public Integer insertTimeAccount(int employeeNum, Integer serverNum, String instanceName, String date, int minutesWorked, int clientDescNum)
    {
        Object ref = this.getLocalHome("ejb/TimeAccount" );
        try
         {
//  *******************  Exception Thrown Here                                        ******                 
            TimeAccountHomeLocal home = (TimeAccountHomeLocal)ref;
            TimeAccountLocal local = home.create( employeeNum, serverNum,        instanceName,  date,  minutesWorked, clientDescNum);
            timeAccountPK = (Integer)local.getPrimaryKey();
        }
         catch (Exception e)
        {
            throw new EJBException(e);
        }
        return timeAccountPK;
    }
    public void removeTimeAccount()
    {
        try
        {
            if (timeAccount != null)
                timeAccount.remove();
        }
        catch (Exception e)
        {
            throw new EJBException(e);
        }
    }
    public void setTimeAccount(Integer primaryKey)
    {
        Object ref = this.getLocalHome("TimeAccount" );
        try
         {
            TimeAccountHomeLocal home = (TimeAccountHomeLocal)ref;
            timeAccount = home.findByPrimaryKey(primaryKey);
         }
         catch (Exception e)
        {
            throw new EJBException(e);
        }
    }
    protected Object getLocalHome(String name)
    {
        try
        {
            Context jndiContext = new InitialContext();
            Object ref = jndiContext.lookup(name);
            return ref;
        }
        catch (NamingException e)
        {
            throw new EJBException(e);
        }
    }
    public ArrayList getTimeAccountView(String date , int employeeNum)
    {
        ArrayList viewList = new ArrayList();
        TimeAccountBean taBean = null;
        System.out.println("getTimeAccountView() date="+date+" employee number = "+employeeNum);
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet result = null;
        try {
          con = this.getConnection();
          ps = con.prepareStatement("Execute time_records  ? , ?");
          ps.setString(2, date);
          ps.setInt(1, employeeNum);
          result = ps.executeQuery();
          while (result.next())
          {
              taBean = new TimeAccountBean();
              taBean.setPrimaryKey(new Integer(  result.getInt("record#") ));
              taBean.setFirstName(result.getString("FirstName"));
              taBean.setLastName(result.getString("lastName"));
              taBean.setServerName(result.getString("serverName"));
              taBean.setDate(result.getString("Date"));
              taBean.setInstanceName(result.getString("instance"));
              taBean.setMinutesWorked(new Integer( result.getInt("minutes_worked")  ));
              taBean.setWeekNumber(new Integer(result.getInt("week#")));
              taBean.setClientDesc(result.getString("description"));
              viewList.add(taBean);
          }
          return viewList;
        }
        catch (SQLException se)
        {
          throw new EJBException (se);
        }
        finally {
          try {
            if (result != null) result.close();
            if (ps != null) ps.close();
            if (con!= null) con.close();
          } catch(SQLException se) {
            se.printStackTrace();
          }
        }
    }
    private Connection getConnection() throws SQLException
    {
        try
        {
            Context jndiCntx = new InitialContext();
            DataSource ds =(DataSource)jndiCntx.lookup("java:comp/env/jdbc/Admin");
            Connection conn = ds.getConnection();
            conn.setCatalog("administration");
            return conn;
        }
        catch (NamingException ne)
        {
            throw new EJBException(ne);
        }
    }
 
LocalHome Interface to Entity Bean:
 
package dbadirect.administration.timekeeping;
import javax.ejb.*;

public interface TimeAccountHomeLocal extends EJBLocalHome
{
      public TimeAccountLocal create(int employeeNum, Integer serverNum,
      String instance, String date, int mworked, int clientDescNum) throws  CreateException;
      public TimeAccountLocal findByPrimaryKey(Integer primaryKey) throws   FinderException;
}
 
Local Component Interface to Entity Bean:
 
package dbadirect.administration.timekeeping;
import javax.ejb.EJBLocalObject;
import javax.ejb.EJBLocalHome;

public interface TimeAccountLocal extends EJBLocalObject
{
        public int getEmployeeNum() ;
        public Integer getServerNum()  ;
        public String getInstanceName()  ;
        public String getDate()  ;
        public int getMinutesWorked()  ;
        public int getClientDescNum()  ;

        public void setEmployeeNum( int employeeNum)  ;
        public void setServerNum( Integer serverNum)  ;
        public void setInstanceName(String instance)  ;
        public void setDate( String date)  ;
        public void setMinutesWorked( int mworked)  ;
        public void setClientDescNum(int cdesc)  ;
 
}


Do you Yahoo!?
Win a $20,000 Career Makeover at Yahoo! HotJobs
<Prev in Thread] Current Thread [Next in Thread>
  • ClassCast Exception while trying to reference a local home object., John Olmstead <=