• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Current best practice for handling collections of EntityBeans

 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18300
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34973
378
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic