• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem about ejbFindAll() of my BMP

 
Ivan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I'm having a problem when calling the findAll() method for my bmp bean.
Seems the class type returned by the findAll() method is not what I expect. I use lomboz eclipse to generate my code and Jboss4.0.2 to run my program. Also, 'Tracking' is a table of mySQL4 db.
Here is the abstract of my bmp and jsp calling program.

[bmp]
.....
public java.util.Collection ejbFindAll() {
Vector vector = new Vector();
DataSource dataSource = getDataSource();
Connection connection = null;
PreparedStatement statement = null;
ResultSet result;
TrackingData tracking = null;
try {
connection = dataSource.getConnection();
statement = connection.prepareStatement("SELECT catid, docid, trackdate FROM Tracking");
result = statement.executeQuery();
while(result.next()) {
tracking = new TrackingData();
tracking.setCatid(result.getString(1));
tracking.setDocid(result.getString(2));
tracking.setTrackdate(result.getDate(3));
vector.add(tracking);
}
result.close();
return vector;
} catch (Exception se) {
throw new EJBException("Could not find record from DB: "
+ se.getMessage()+ se.getStackTrace());
} finally {
try {
statement.close();
connection.close();
} catch (Exception e) {}
}
}
......

[jsp]
......
<%
try {
//Lookup User Home interface
Context context = new InitialContext();
Object ref = context.lookup("ejb/Tracking");
TrackingHome trackingHome = (TrackingHome)
PortableRemoteObject.narrow(ref, TrackingHome.class);

Collection bean ;
Iterator iterator ;
TrackingData trackingData;
String catid;
String docid;
Date trackDate;
try {
bean = trackingHome.findAll();
iterator = bean.iterator();
while(iterator.hasNext()) {
trackingData = (TrackingData)iterator.next(); <--- Exception
catid = trackingData.getCatid();
docid = trackingData.getDocid();
trackDate = trackingData.getTrackdate();
out.println("<br>");
out.println("Catid:"+catid+" ");
out.println("Docid:"+docid+" ");
out.println("Trackdate:"+trackDate+"\n");
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace(System.out);
}
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(e.getStackTrace());
}
%>

......
There is an object returned by the remote 'findAll()' function.
However, when I call "trackingData = (TrackingData)iterator.next();", it throws "java.lang.ClassCastException: $Proxy109"

Here is the error message details,
$Proxy109
java.lang.ClassCastException: $Proxy109
at org.apache.jsp.TestTracking_jsp._jspService(org.apache.jsp.TestTracking_jsp:79)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
1at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
at java.lang.Thread.run(Thread.java:595)


Do you have any ideas ? please kindly help.
Thanks in advance.

Ivan
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may be a classloader problem. Is all your code deployed in multiple archive files?
 
Ivan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. My code are all deployed in a .ear file

Ivan
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you rebuilt and redeployed, but are getting the same exception? If so, do some debugging. Invoke this:
 
Ivan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help.
it returns ..

The name of the class is: $Proxy107

what's that !!!?? Is it a bug in JBOSS4.0.2 ?
Moreover, the result is normal for CMP.
As the ejbFindAll() is generated internally for CMP,
I have no way to compare the difference.

Ivan
 
Marco Barenkamp
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Your ejbfind Method MUST NOT return all fields of your ejb.
ejbfinder Methods for BMP should ONLY return the PKs.
(that is exactly what the generated ejbfind method with CMP would do).
The EJB Container will then invoke ejbActivate for each EJB in order
to initialize the other fields.

Proxy$0 or sth will always be returned by JBoss, because JBoss (in contrast to other AppSrvs) does not generate class files at deploytime
for your home and component interfaces ,
but uses Dynamic Proxies!

Bye.
 
Ivan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
According to your suggestion, I have already changed my code as follows,
but the same problem resume...
We use the TrackingPK class (generated by xdoclet) to store the key values and the primary key is composed of 2 fields (i.e. catid + docid)


[BMP:ejbFindAll()]
.....
public java.util.Collection ejbFindAll() {
ArrayList list = new ArrayList();
DataSource dataSource = getDataSource();
Connection connection = null;
PreparedStatement statement = null;
ResultSet result;

TrackingPK tracking = null;


try {
connection = dataSource.getConnection();
statement = connection
.prepareStatement("SELECT catid, docid, trackdate FROM Tracking");
result = statement.executeQuery();
while(result.next()) {
tracking = new TrackingPK();
tracking.setCatid(result.getString(1));
tracking.setDocid(result.getString(2));

list.add(tracking);

}
result.close();

return list;

} catch (Exception se) {
throw new EJBException("Could not find record from DB: "
+ se.getMessage()+ se.getStackTrace());
} finally {
try {
statement.close();
connection.close();
} catch (Exception e) {}
}
}
.....

[JSP]
......
<%
try {
Context context = new InitialContext();
Object ref = context.lookup("ejb/Tracking");
TrackingHome home = (TrackingHome) PortableRemoteObject.narrow(ref,
TrackingHome.class);
Collection bean ;
Iterator iterator ;
Tracking tracking;

bean = home.findAll();
iterator = bean.iterator();
Integer docid, catid;
Date trackdate;
while(iterator.hasNext()) {
out.println("<br>The name of the class is: " + iterator.next().getClass().getName());
}

} catch (Exception e) {
out.println(e.getMessage());
} finally {
out.println("</body></html>\n");
out.close();
}

%>
......


and here is the output of the JSP,

The name of the class is: $Proxy189
The name of the class is: $Proxy189
The name of the class is: $Proxy189
The name of the class is: $Proxy189


Can the BMP finder method allow returning collection of TrackingPK ?

Thanks,
Ivan
 
Marco Barenkamp
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

There must be another error. If you want to, you can email your EJB
and I will take a look.
 
Marco Barenkamp
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your EJB code is correct, but JSP code is not.
You are expecting TrackingPK instances from your finder Method, but that is wrong.
You are getting Tracking instances of course, becuase findermethods are intended
to return EJB Objects, no PKs.
Think of the following:
Your findByPrimaryKey Method returns an EJBObject, but the appropriate
ejbFindByPrimaryKey method returns the PK.The container will then initialize
the EJB with that key. That's what I meant in my previous answer regarding
to the invocation of ejbActivate.

Bye.
 
Ivan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. Macro,
thanks for your help, but I don't quite understand your point.
If my bean code is correct, but JSP is wrong.
How can I change my JSP code so as to make it work again ?

Ivan
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!