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.
okay 2 enhancements I see here already, - instead of passing orderId to addOrderItem method I can do like, int orderId = ((LocalOrder)context.getEJBLocalObject()).getId(); in the addOrderItem() method - use environment to get that lookup string for the OrderItem bean so I can avoid hardcoding... Regards Maulin
hi, There are many ID generation patterns. you may want to check them. Or, u can use database sequence. I am not sure if u can use context.getId() to create a new ID. It returns the value of an existing ID.
Hi varanasir I already referred to all methods available like UUID and all. This is the approach I was having in my web based application and now I am trying to move to J2EE with similar logic. The sequencing works with Oracle. MySQL has auto-increment field type and I am investigating approach of using sequences in DB where I am bound to Oracle etc...Currently reading, http://www.theserverside.com/patterns/thread.jsp?thread_id=5285#16721
Also, I am not performing getId() on context. I am getting the localObject, casting it properly and then invoking business method getId() I have put there...I didn't include that in my description to avoid unnecessary confusion... Regards Maulin
I promise I will be the best, most loyal friend ever! All for this tiny ad: