Hi all,
I've gotta problem regarding entity beans.
I have a simple entity bean as follows:
Remote:
package test;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
import java.io.Serializable;
public interface TestEntity extends EJBObject, Cloneable, Serializable
{
}
EJB:
package test;
import java.util.Vector;
import javax.ejb.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
public class TestEntityEJB implements EntityBean
{
public String name;
private EntityContext context;
public String ejbCreate(String n) throws CreateException
{
return "";
}
//----------------------------------------------------------------------
public void setEntityContext(EntityContext context)
{ this.context = context;
try {
makeConnection();
} catch (Exception ex) {
throw new EJBException("Unable to connect to database. " +
ex.getMessage());
}
}
//----------------------------------------------------------------------
public void unsetEntityContext()
{
}
//----------------------------------------------------------------------
public void ejbPostCreate(String n)
{
}
//----------------------------------------------------------------------
public void ejbRemove()
{ try
{ deleteRow ("");
}
catch (Exception ex)
{ throw new EJBException("ejbRemove: " + ex.getMessage());
}
}
public void remove()
{ try
{ deleteRow ("");
}
catch (Exception ex)
{ throw new EJBException("ejbRemove: " + ex.getMessage());
}
}
//----------------------------------------------------------------------
public void ejbActivate()
{ name = (String)context.getPrimaryKey();
}
//----------------------------------------------------------------------
public void ejbPassivate()
{ name = null;
/*productID = null;
productName = null;*/
}
//----------------------------------------------------------------------
public void ejbLoad()
{ try
{ loadRow();
}
catch (Exception ex)
{ throw new EJBException("ejbLoad: " + ex.getMessage());
}
}
//----------------------------------------------------------------------
public void ejbStore()
{ try
{ storeRow();
}
catch (Exception ex)
{ throw new EJBException("ejbLoad: " +
ex.getMessage());
}
}
//==========================================================================
====
public String ejbFindByPrimaryKey(String primaryKey) throws
FinderException
{ boolean result;
try
{ result = selectByPrimaryKey(primaryKey);
}
catch (Exception ex)
{ throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage());
}
if (result)
{ return primaryKey;
}
else
{ throw new ObjectNotFoundException ("Row for id " + primaryKey + "
not found.");
}
}
public Vector ejbFindAll() throws FinderException
{ Vector result;
try
{ result = selectAll ();
}
catch (Exception ex)
{ throw new EJBException("ejbFindAll " + ex.getMessage());
}
if (result.size()==0)
{ throw new ObjectNotFoundException("No rows found.");
}
else
{ return result;
}
}
//==========================================================================
====
private boolean selectByPrimaryKey(String primaryKey)
{ return true;
}
private Vector selectAll ()
{ Vector v = new Vector ();
v.addElement("element");
return v;
}
//==========================================================================
====
private void makeConnection() throws NamingException
{
try
{ Context initial = new InitialContext();
}
catch (Exception ex)
{ System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
}
}
private void loadRow()
{
}
private void insertRow(String n)
{
}
private void deleteRow (String id)
{
}
private void storeRow ()
{
}
}
Home:
package test;
import java.util.Vector;
import java.rmi.RemoteException;
import javax.ejb.*;
public interface TestEntityHome extends EJBHome
{ public TestEntity create (String n)
throws RemoteException, CreateException;
public TestEntity findByPrimaryKey (String name)
throws FinderException, RemoteException;
public Vector findAll ()
throws FinderException, RemoteException;
}
All my finder method findall () in the home does is return a Vector which
contains a single String object "element".
I test this finder method with client code:
try
{ Context initial = new InitialContext();
Object objref = initial.lookup("TestEntity");
TestEntityHome home =
(TestEntityHome)PortableRemoteObject.narrow(objref, TestEntityHome.class);
Vector v = home.findAll ();
}
catch (Exception ex)
{ throw new EJBException(ex.getMessage());
}
the code Vector v = home.findAll (); causes the catch block to be executed
and I get the following exception:
javax.ejb.EJBException: RemoteException occurred in server thread; nested
exception is:
java.rmi.RemoteException: Unknown Exception/Error thrown by EJB method.;
nested exception is:
java.lang.ClassCastException: java.util.ArrayList
at test.TestClient.findTestEntities(TestClient.java:148)
at test.TestClient.testTestEntity(TestClient.java:54)
at test.TestClient.<init>(TestClient.java:23)
at test.TestClient.main(TestClient.java:28)
Exception in thread "main"
java.lang.ClassCastException: java.util.ArrayList - does this mean that I
can't use Vectors in the returns of finder methods???
Your comments will be greatly appreciated.
Regards,
Dinesh.
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".