aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Current best practice for handling collections of EntityBeans 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 "Current best practice for handling collections of EntityBeans" Watch "Current best practice for handling collections of EntityBeans" New topic
Author

Current best practice for handling collections of EntityBeans

Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Hi all,

It's been ages since I've last used EJB's so I'm kind of lost here. The question is, how should one approach the need to maintain a collection of EntityBeans?

Here's a scenario, a classical online shop:

- Users can add items to a shopping cart
- The shopping cart should not be persisted into a database
- The items themselves are of a single type of EntityBean

Now, I suppose this could be handled using CMR if the shopping carts would be EntityBeans as well, but what would be a good way to handle this with the ShoppingCart being a Stateful SessionBean, for example? How about if the shopping cart is just a POJO (a java.util.Vector, for example)?

Thanks.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
you can have a Order(Bean) for Item(Bean)-s bought by a Customer(Bean)
thereby resulting in

OrderBean{
create(CustomerBeanLocal custLocal, Collection itemBeans);
//cmr, collection
Collection getItems();
//cmr, single value
CustomerLocal getCustomer();
}

Customer{
//cmr, collection
Collection getOrders(); //returns OrderBean collection
setOrders(Collection orderbeanLocals);
}
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Karthik Guru:
you can have a Order(Bean) for Item(Bean)-s bought by a Customer(Bean)
thereby resulting in ...

Ah. But isn't this an EntityBean-has-EntityBeans situation? What if I don't want that? What if I want a non-persistent object to have a collection of EntityBeans?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

Just for aesthetic reasons, if you're using EJBs for the items in a shopping cart, you might as well be consistent and use an EJB shopping cart as well. If nothing else, it'll probably help in maintainability because you're usig a consistent code model.

I doubt that as your project progresses you'll want to keep the cart as a simple vector, anyway. Usually as you progress through checkout, you'll be accumulating additional shipping, ordering and billing info. So at that point you'll become a complex custom class regardless.


Customer surveys are for companies who didn't pay proper attention to begin with.
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
Originally posted by Lasse Koskela:

Ah. But isn't this an EntityBean-has-EntityBeans situation? What if I don't want that? What if I want a non-persistent object to have a collection of EntityBeans?


ok. I'm not sure if i got your question. Tell me this, when you fetch the item beans in the first place for display, guess you already have value objects for those. Depending upon what the customer adds to the shopping cart, you will add them to a vector of item value objects and hold them as instance variable in your StatefulSession bean shopping cart right?

Later when you persist, you will persist them in a Customer-Items table / whichever way you track an order, looking up those ItemBeanLocals and CustomerLocal?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Karthik Guru:
Tell me this, when you fetch the item beans in the first place for display, guess you already have value objects for those. Depending upon what the customer adds to the shopping cart, you will add them to a vector of item value objects and hold them as instance variable in your StatefulSession bean shopping cart right?
Yep. That's how I probably would do it.
This "what if" stuff is for purely self-educational purposes. I don't have a project that actually requires this stuff -- I'm just trying to update my world view about EJB's after a rather long period of not having to deal with them beasts
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30954
    
158

Since we are in the land of "what if"...

If I had a non-persistent shopping cart, I would just keep the keys (which are regular java classes.) Then I would look up the entity beans themselves as needed.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Current best practice for handling collections of EntityBeans