aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Running SQL in an EJB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Running SQL in an EJB" Watch "Running SQL in an EJB" New topic
Author

Running SQL in an EJB

richard marais
Ranch Hand

Joined: Aug 20, 2001
Posts: 30
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
Marcos Maia
Ranch Hand

Joined: Jan 06, 2001
Posts: 977
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

Joined: Aug 20, 2001
Posts: 30
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?
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
The finder methods don't belong in the remote interface, they belong in the home interface.
richard marais
Ranch Hand

Joined: Aug 20, 2001
Posts: 30
How do you access the home interface methods from a JSP then?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Aug 20, 2001
Posts: 30
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);
anup vachali
Ranch Hand

Joined: Oct 17, 2000
Posts: 54
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running SQL in an EJB