File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes am I doing this correct way? 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 "am I doing this correct way?" Watch "am I doing this correct way?" New topic

am I doing this correct way?

Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
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 Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
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...
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
no takers yet?

Joined: Jun 22, 2001
Posts: 6
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.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
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,

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...
I agree. Here's the link:
subject: am I doing this correct way?
It's not a secret anymore!