File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes BMP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "BMP" Watch "BMP" New topic
Author

BMP

ALaxmi Shankaran
Greenhorn

Joined: Feb 23, 2004
Posts: 26
Hi all
First time i am using BMP EJB
In one BMP i have written one Finder method as ejbFindCompanyRecords which returns a Collection
Also i have declared that in the home interface.
public Collection ejbFindCompanyRecords() throws FinderException,RemoteException;
From servlet ,I am calling this finder method as
Collection companyCollection=companyHome.findCompanyRecords();

it compiles well but while run time it gives me casting exception
I am confused wether to create the remote instance or not...
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
You might need to share your code with us and provide the error stack trace. However I expect that you get a reference to the home interface in this way:

Using the local home interface. Another alternative is to narrow rather than casting:

Using the remote interface. My guess is that for whatever reasons your syntax for casting/narrowing is maybe wrong. But as I said you must share your code with us. Let me know if you need any more help.
Regards.


I think, therefore I exist -- Rene Descartes
ALaxmi Shankaran
Greenhorn

Joined: Feb 23, 2004
Posts: 26
My EJB method is as like --

public Collection ejbFindCompanyRecords(){
Collection companyCollection=new ArrayList();


try {
-- got connection
-- created statement
-- resultset
-- populated collection
-- closed resources used

} catch(SQLException sqlExc){

} catch(NamingException namingExc){

}finally{
try{
-- closed resources if any open
}catch(SQLException sqlExc){

}
}
return companyCollection;
}

My Home interface is as like --

public Collection findCompanyRecords()
throws FinderException,RemoteException;


My deployment descriptor as like --

<entity>
<display-name>Company</display-name>
<ejb-name>Company</ejb-name>
<home>company.CompanyHome</home>
<remote>company.Company</remote>
<ejb-class>company.CompanyEJB</ejb-class>
<persistence-type>Bean</persistence-type>
<prim-key-class>java.lang.Integer</prim-key-class>
<reentrant>False</reentrant>
<resource-ref>
-- proper resource reference here
</resource-ref>
</entity>



My servlet part of code --

InitialContext context = new InitialContext();
Object ref=context.lookup("Company");
CompanyHome companyHome=(CompanyHome)PortableRemoteObject.narrow(ref,CompanyHome.class);
Collection companyCollection=companyHome.findCompanyRecords(); // Here it gives exception ..........

I am getting exception as ....

java.lang.ClassCastException: java.lang.String
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704

-- got connection
-- created statement
-- resultset
-- populated collection
-- closed resources used

And I suppose you populate your collection with a list of primary keys (Integer objects I suppose).
Balamaci Serban
Ranch Hand

Joined: Mar 16, 2005
Posts: 49
'fraid I'm here with some questions and not answers
Ok, i know that in the Bean class in the Collection findCompanyRecords() method, the returned collection is one of Integers.
Ok, but in the CompanyHome we have Collenction findCompanyRecords() which return what? It returns a colection of say CompanyRemote extends EJBObjects objects?

Is this correct?
[ March 21, 2005: Message edited by: Balamaci Serban ]
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
Yes it will return a collection of remote interfaces.
Regards.
Balamaci Serban
Ranch Hand

Joined: Mar 16, 2005
Posts: 49
Cheers guys,
Ok, my next question will somehow deviate from the initial question but it still in the domain of returned Collection and findAll getter.

By using a session facade pattern.

On the client
1.lookup Session home;
2.create remote interface named say remote_conection
3.get all the records to be just displayed. remote_conection.getAllRecords()

Is the wright way to go on this?

We would both have on the server and on the client the class Record:
public class Record implements Serializable
{
private int id;
private String name;
private double value;

public Record(int id_t,String name_t,double value_t);
public int getId();
.
.
.
}


On the server side in the SessionBean

public Collection getAllRecords()
{
Vector v=new Vector();
//get home of the records entity bean;
Iterator it=home.findCompanyRecords().iterator();

while(it.hasNext())
{

//getting from the Company Entity bean the records
remote=(CompanyRemote) javax.rmi.PortableRemoteObject.narrow(i.next(),CompanyRemote.class);

//fillup the Record class
Record rcd=new Record(remote.getId(),remote.getName(),remote.getValue());
v.add(rcd);
}
return v;
}

And on the client side we need to suply the Record class and begin the whole thing again

Iterator it=remote_conection.getAllRecords().iterator();
while(it.hasNext())
{
Record r=(Record)i.next();
}

Now is it just me or some other people feel entangled and get to confuse some things with other?
I know the session facade is not there to just mimic all the entity beans methods, but besides the bussines stuff employed in the session bean, there will be times when you would need to return some collections to the client, the records in the database. So the question: is that how you would do it? Another Serializable class that you need time to fill up with the records returned by the entity bean and gets passed to the client(it seems to me like doing something twice)?
[ March 22, 2005: Message edited by: Balamaci Serban ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BMP
 
Similar Threads
Difference in CMP/ BMP finder methods
ejb question
Developing EJB 2.0 Components by Tulachan
choosing between home business methods and finders
help required specifying Transation attributes for this scenario