cancel
Showing results for 
Search instead for 
Did you mean: 

Delete records vis EJB

Former Member
0 Kudos

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.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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

Answers (2)

Answers (2)

Former Member
0 Kudos

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

Former Member
0 Kudos

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

Former Member
0 Kudos

Hai Sabbir,

Have you created any java helper classes , in your project make sure that classes implements java.io.Serializable Interface (i.e all the properties must be serializable).

regards,

Naga

Vlado
Advisor
Advisor
0 Kudos

Please refer to thread for the solution.

-Vladimir

former_member182372
Active Contributor
0 Kudos

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.

Former Member
0 Kudos

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.

former_member182372
Active Contributor
0 Kudos

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.

Former Member
0 Kudos

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.