on 04-18-2007 2:44 AM
Hello experts
I have created entity and session beans to conduct database transactions. My database table has the following fields
room_id -- string
room_name -- String
For the entity bean RoomsBean, the default methods ejbCreate(String a, String b) and ejbfindbyPrimaryKey(String a) are created that I uase to create a record in the database and find a record in the database.
Now, I need a method to delete records from databse. How do I create that method? Is there a default method that I can use to delete a record? Do I have to create a method and use EJB QL to do it?
If I have to create a method, then where do I create it as the options under ejb methods are
1. create methods
2. find methods
3. home methods
4. select methods
please help.
Hai,
public String DeleteRoom(String room_id) {
String message = "";
try {
InitialContext ctx = new InitialContext();
remotehome = (RoomsHome) ctx.lookup("java:comp/env/ejb/RoomsBean");
<b>remote=remotehome.findByPrimaryKey(room_id);
remote.remove();</b>} catch (RemoteException e) {
message = e.getMessage();
} catch (NamingException e) {
message = e.getMessage();
} catch (RemoveException e) {
message = e.getMessage();
}
return message;
}
regards,.
Naga
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hai ,
create a Finder method ejbFindBySixMnths().
expnad your ejb-jar.xml>select Enterprisebeans tab>expnad entitybean >select your bean>expnad query-->select the finder method write the ejb ql to find record,
delete the records , as told above.
regards,
Naga
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
dear naga,
That was very helpful. I have created a finder method ejbFindAllRooms()
The following is the EJB QL
select object(r) from Rooms r
I get the follwing error when I call it from a portal component
com.sap.engine.services.rmi_p4.exception.P4BaseRuntimeException: I/O operation failed : java.io.NotSerializableException: com.sap.engine.services.ejb.entity.finder.EJBLocalCollection :
at com.sap.engine.services.rmi_p4.server.P4ObjectBrokerServerImpl.getException(P4ObjectBrokerServerImpl.java:926)
at com.sap.engine.services.rmi_p4.reflect.LocalInvocationHandler.replicateReturnValue(LocalInvocationHandler.java:115)
at com.sap.engine.services.rmi_p4.reflect.LocalInvocationHandler.invokeInternal(LocalInvocationHandler.java:90)
at com.sap.engine.services.rmi_p4.reflect.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:50)
at $Proxy180.getAllrooms(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at com.sap.engine.services.ejb.session.stateless_sp5.ObjectStubProxyImpl.invoke(ObjectStubProxyImpl.java:187)
at $Proxy181.getAllrooms(Unknown Source)
at com.watercorp.BMS.test.EJBTest.doContent(EJBTest.java:120)
at com.sapportals.portal.prt.component.AbstractPortalComponent.serviceDeprecated(AbstractPortalComponent.java:209)
at com.sapportals.portal.prt.component.AbstractPortalComponent.service(AbstractPortalComponent.java:114)
at com.sapportals.portal.prt.core.PortalRequestManager.callPortalComponent(PortalRequestManager.java:328)
... 29 more
Caused by: java.io.NotSerializableException: com.sap.engine.services.ejb.entity.finder.EJBLocalCollection
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1054)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:278)
at com.sap.engine.services.rmi_p4.reflect.LocalInvocationHandler.replicateReturnValue(LocalInvocationHandler.java:110)
... 42 more
Hi Sabbir,
Home interface <a href="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/ejb/EJBHome.html">EJBHome</a> has method remove which takes primaryKey object.
Bes tregards, Maksim Rashchynski.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Maksim,
Thank you for that. I have now created a method called DaleteRoom(String room_id) in my session bean and added the follwoing code
public String DeleteRoom(String room_id) {
String message = "";
try {
InitialContext ctx = new InitialContext();
remotehome = (RoomsHome) ctx.lookup("java:comp/env/ejb/RoomsBean");
remotehome.remove(room_id);
} catch (RemoteException e) {
message = e.getMessage();
} catch (NamingException e) {
message = e.getMessage();
} catch (RemoveException e) {
message = e.getMessage();
}
return message;
}
What I waht to know is
remotehome = (RoomsHome) ctx.lookup("java:comp/env/ejb/RoomsBean");
does this look correct to you? I use "java:comp/env/ejb/RoomsBean" to call the local interface. Is it still correct for the remore interface. Any suggestion to this piece of code will be greatly appreciated.
thank you.
Sabbir, you got the answer here (From Sergey):
<i>
You can find JNDI name in NetWeaver Administration of EJB module.
Help.sap.com:
If you are accessing your beans locally, note that the local interface of the bean will be registered in the JNDI with a localejbs/ prefix in front of the JNDI name. That is, to look up your bean locally, use localejbs/<jndi-name>. To look up your bean remotely, use <jndi-name>.
</i>
Check for more info http://help.sap.com/saphelp_nw04/helpdata/en/86/2ccbdefb8f5843958a11062e19fc1d/content.htm
Best regards, Maksim Rashchynski.
Maksim,
You post was very helpful for me. I am now using the remote interface to remove a record from the database. Now, I need to write a method to delete all records and
another method to delete some records that are older than 6 months.
how do I do this? If I create a new method to do it , which section should it go under as the options are
a. create method
b. finder methods
c. home methods
d. select methods
And how the EJB QL would look like?
thank you.
User | Count |
---|---|
87 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.