Cant add 3 addresses to DB in a loop?

Using the code shown below, I am trying to add 3 different addresses to the database. The code exists in a session ejb. It will only add one address.

What is also odd is that it adds the address with the first ID number, but the second type & isPrimary assignment(type=WB & primary=0). Almost like it has changed the original record.

Why will it not add 3 distinct address records? I know it loops thru the code 3 times but it is not adding the 3 records. This code worked when it existed in a servlet, but since I moved it to an EJB, it will not work.

here is the loop code:

for (int i=0; i<3; i++){

if (i == 0){

addr.setAddresstype("WC");

addr.setIsprimary(1);

}elseif (i == 1){

addr.setAddresstype("WB");

addr.setIsprimary(0);

}else{

addr.setAddresstype("WS");

addr.setIsprimary(0);

}

//get next available addressId

BIaddressId = keyMgrBean.getNextKey("address");

addr.setAddressId(BIaddressId);

// update the database using the Facade EJB

addressFacade.create(addr);

}// end loop 3 times

[1738 byte] By [Perryiera] at [2007-11-27 11:13:21]
# 1

I can't really tell for certain what's going on without the rest of your code (please don't post it!)

I observe that you're using the same object, addr, through all your iterations. Does addressFacade.create() behave as expected when handed the same object three times with just the attributes changed?

Had I had the problem, I might experiment with changing the code (no, a copy of the code, not the original), for example looping once or twice only, and see how that behaves. It just might give a clue in the end. If somehow you can separate the three times through the loop so you can inspect the database in between, you'd be able to see whether it actually changes the original record or not.

OleVVa at 2007-7-29 14:00:22 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Your question - Does addressFacade.create() behave as expected when handed the same object three times with just the attributes changed? - Is a good one and is what I am trying to determine.

As a test I created another Session bean that has 3 methods (AddBillAddress, AddShipAddress & AddCtlgAddress). I pass the same addr object to each one. instead of looping 3 times, I call the 3 methods. inside each method I am getting and assigning a new Key ID value to the addr object..

The same thing happens. It seems to only add 1 address, and it is the second one in the series of method calls.

Again, this code worked when I used it in a servlet. is the persistence object functioning differently when the .create method is called in an EJB vs in a servlet?

Perryiera at 2007-7-29 14:00:22 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

I managed to get it to add 3 addresses by calling 3 separate methods in the servlet.

newCustomerMB.registerCustomer(newCust, address);

newAddressMB.addBillAddress(address);

newAddressMB.addShipAddress(address);

This seems counter intuitive. Don't I want the method calls to be in an EJB to take advantage of the EJB container transaction control? It appears that I either have to sacrifice transaction control or put in several lines of code to build 3 separate address objects so that they can all be added in the EJB (even this is untested, It may still not work).

I must be missing something.

Perryiera at 2007-7-29 14:00:22 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...