• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Running SQL in an EJB

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
1) I have setup a CMP entity EJB in VisualAge, when I run the test client it can return data from the databse with no problems. However when I call the getter & setter methods of the bean from a JSP it returns only the initialized values and not the values from the database. I am calling ejbCreate to try and locate the rows primary key but with no success.
2) Where would I put SQL statements in the CMP bean? & how would I do this?
Thanks
 
Ranch Hand
Posts: 977
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notice than if you�re using CMP ejb�s you don�t have to code sql statements to do so you need to use BMP ejb.
Now to recover information from the DB using CMP ejb all you need to do is using the finder methods that you have to define in your home interface and implement in the ejb class.
just doing an append here:
for the ejb 2.0 specification you have the implementation of QL language that you can code at your ejb-jar.xml and have something like sql code in the CMP ejbs.
[This message has been edited by Marcos Maia (edited August 21, 2001).]
 
richard marais
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi me again,
I'm having problems accessing the database through a CMP bean.
The bean has a default findByPrimaryKey method in the home interface already, when I run the test client I invoke this method before the remote interface methods, and it work fine. My problem is that I do not know how to access this method from my JSP. I can access the remote interface methods but not the finder methods. When I add the default findByPrimaryKey method to the remote interface I get a compilation error (Method Ory_menu.findByPrimaryKey(Ory_menuKey) has no matching method findByPrimaryKey in bean class Ory_menuBean). I create a findByPrimaryKey in the bean class, but don't know what it should do (return somthing?). How will this locate the relevent record?
Also id I want to create other finder methods (where clause), do I put it in the BeanFinderHelper interface? anywhere else?
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The finder methods don't belong in the remote interface, they belong in the home interface.
 
richard marais
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you access the home interface methods from a JSP then?
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To access the methods of the remote interface you must first find the object using the initial context:
Object o = ctx.lookup("MyEntity");
Home aHome = (MyEntityHome)PortableRemoteObject.narrow(o, MyEntityHome.class);
// now we have a reference to a home with which we can find our data..
MyEntity aRemote = aHome.findByPrimaryKey(pk);
// now we have a remote
aRemote.setSomeAttribute(value);
 
richard marais
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I tried to do what Sven says above... here is my code in a test JSP:
SessionOryBean sb = new SessionOryBean();
sb.ejbCreate();
out.println("

getSessionContext = "+sb.getSessionContext());
out.println("
getValue = "+sb.getValue());
javax.ejb.SessionContext ctx = sb.getSessionContext();

Object o = ctx.lookup("Ory_MenuBean");
Home aHome = (Ory_MenuHome)PortableRemoteObject.narrow(o, Ory_MenuHome.class);
// now we have a reference to a home with which we can find our data..
Ory_MenuBean aRemote = aHome.findByPrimaryKey(pk);
// now we have a remote
out.println("

getRes_id = "+aRemote.getRes_id());
-------------------------------------------
Cannot find the method called lookup(String). Have I defined the session context ctx correctly?
-------------------------------------------
Here are the error I got:
com.ibm.servlet.engine.webapp.WebAppErrorReport: D:\Program Files\IBM\VisualAge for Java\ide\project_resources\IBM WebSphere Test Environment\temp\JSP1_0\default_app\_EJBTester_xjsp_jspsrc_1153903185.java:102: Method lookup(java.lang.String) not found in interface javax.ejb.SessionContext.
Object o = ctx.lookup("Ory_MenuBean");
^
D:\Program Files\IBM\VisualAge for Java\ide\project_resources\IBM WebSphere Test Environment\temp\JSP1_0\default_app\_EJBTester_xjsp_jspsrc_1153903185.java:103: Class Home not found.
Home aHome = (Ory_MenuHome)PortableRemoteObject.narrow(o, Ory_MenuHome.class);
^
D:\Program Files\IBM\VisualAge for Java\ide\project_resources\IBM WebSphere Test Environment\temp\JSP1_0\default_app\_EJBTester_xjsp_jspsrc_1153903185.java:103: Class Ory_MenuHome not found.
Home aHome = (Ory_MenuHome)PortableRemoteObject.narrow(o, Ory_MenuHome.class);
^
D:\Program Files\IBM\VisualAge for Java\ide\project_resources\IBM WebSphere Test Environment\temp\JSP1_0\default_app\_EJBTester_xjsp_jspsrc_1153903185.java:103: Undefined variable or class name: PortableRemoteObject
Home aHome = (Ory_MenuHome)PortableRemoteObject.narrow(o, Ory_MenuHome.class);
^
D:\Program Files\IBM\VisualAge for Java\ide\project_resources\IBM WebSphere Test Environment\temp\JSP1_0\default_app\_EJBTester_xjsp_jspsrc_1153903185.java:105: Class Ory_MenuBean not found.
Ory_MenuBean aRemote = aHome.findByPrimaryKey(pk);
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is relevant to a weblogic server...but I am sure there must be something akin to this if my assumption about u using WebSphere is right.
Try this:
In ur JSP:
YourEJBHome xyzHome = (YourEJBHome)getHome(JNDI-Bean-Name);
//get the remote
Ory_MenuBean aRemote = aHome.findByPrimaryKey(pk);
//call the method in ur remote
getRes_id = "+aRemote.getRes_id();
...
...
-------
public EJBHome getHome(String beanName) {
try {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL, serverURL);
Context ctx = new javax.naming.InitialContext(p);
// To look for the home bean
EJBHome objEJBHome = (EJBHome) ctx.lookup(beanName);
return objEJBHome;
}catch (NamingException ne) {
//do something
}
}

HTH,
Anup
[This message has been edited by anup vachali (edited August 22, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic