Hi all,
Here I am again with some design questions...
Please look at the following code that I have prepared. I have the following tables in the database,
Order(id, name, primary key (id));
OrderItem (orderId, id, orderItemName, primary key (orderId,id), foreign key Order(id));
I am just
testing out things so I have kept bare minimum structure here. I have the following code written for Order Entitybean. I am using CMP and relationship between Order and OrderItem beans here.
As you see I am generating OrderItem Id with my custom logic. Actually, I needed to run "select max(id) from orderItem where orderId=?" and then do Max+1 for the new Id but the app server I am using doesn't support
EJB QL 2.1 which has this aggregation functions.
What I want to do is, I want to keep this Id generation logic hidden from the client using OrderBean. I just want them to pass me the orderid and orderItem name for e.g. and thats all. This is due to the fact that OrderItem is weak entity and the client programs don't care how backend has to store Ids.
So, I would put OrderSessionBean that acts as client to this OrderBean and call method like addOrderItem(orderId, orderItemName) and boom...I would have orderItem added in there.
This is what I have so far. My questions are,
1. Does this sound normal or sounds like I am putting hacks?
- The problem is- how do I generate IDs? I have seen example beans but all of them have "natural" ids for e.g. Player has his "name" and none of the examples go into the fact that "how things are done when we have our own ID generation mechanism". I already know some algo/approaches to ID generation as discussed here in some other
thread but what I look for here is "how client program can get IDs to call create() methods"
2. The local home for OrderItem you see in the code. Is it okay to have it that way? Do I need it declared "transient"?
- Here I have to keep that as I have to create new OrderItem and add to the collection but its not a part of OrderBean so it is not supposed to get serialized with the bean state.
3. Here, as you would notice, I am having OrderItem PK consisting of the forieng key orderId. Now, as create() on OrderItem inserts the orderId already in the OrderItem table, is it neccessary to add the OrderItem object in the Collection?
- I guess we need to add it to collection so that container can put FK in OrderItem table automatically but in my case I am already putting it via create()...
ooooooooops. I eneded up writing really long one. Hopefully somebody will have patience...
Thanks a lot.
Maulin.