I only get the last entity bean updated in a loop

Discussions

EJB programming & troubleshooting: I only get the last entity bean updated in a loop

  1. I only get the last entity bean updated in a loop (3 messages)

    in a method of my session bean ,i use a loop to update several entity beans.firstly,I get the entity bean by ejb home's findByPrimayKey method,then get the value object ,set the value oject properties,and then set the value object into ejb remote interface.my problem is that i always got the last entity bean updated.

    can anybody give an answer?
    thanks.
  2. After you get a handle to the home object, start the loop, for each unique key, loop through and findByPrimaryKey. findByPrimaryKey will return you the Remote Object. Set the appropriate values using Setters. This internally would kick off ejbStore everytime and update all your beans.

    Eg:
    //get a handle to the home object
    final AlertHome alertHome = (AlertHome)PortableRemoteObject.narrow(remoteRef, AlertHome.class);
    //for each of the unique id
    for (int i=0; i<alertID.length; i++) {
        mkey = new AlertKey(alertID[i]);
     //create a new primarykey
        System.out.println("*************alert key to find by is " + alertID[i]);
       alert = (Alert)alertHome.findByPrimaryKey(mkey);
    //findByPrimaryKey returns a remote object
       alert.setMyAlertProfileId(null);
    //set the necessary values, this will update each entity bean
  3. Eg:
    //get a handle to the home object
    final AlertHome alertHome = (AlertHome)PortableRemoteObject.narrow(remoteRef, AlertHome.class);
    //for each of the unique id
    for (int i=0; i<alertID.length; i++) {
        mkey = new AlertKey(alertID[i]);
     //create a new primarykey
        System.out.println("*************alert key to find by is " + alertID[i]);
    //WHETHER YOU SHOULD CREATE A NEW VARIABLE FOR EVERY ALERT.
    ALERT alert = (Alert)alertHome.findByPrimaryKey(mkey);
    //findByPrimaryKey returns a remote object
       alert.setMyAlertProfileId(null);
    //set the necessary values, this will update each entity bean
  4. thank jan.
    i review your example and find out that the difference between yours and mine :
    (1) yours declare the remote interface variable in the loop,
    and mine out the loop.
    (2) yours set the properties of ejb directly while mine set it via a value object.

    I don't know whether one of the differnce cause the problem