Hello experts - a question for you!
In EJB 2.0 using CMP and CMR, it seems that for a PK that is composite, with one part being a foreign key to a "parent" entity (in other words an identifying primary key), gives rise to conflicting requirements on the bean. What's the right approach?
For example, I have parent entity Store with a StorePK consisting of storeNum field, and corresponding getStoreNum() / setStoreNum() accessor methods. I also have a child entity Receipt, with a ReceiptPK of (storeNum, receiptNum), so that receipts are identified by store ((1,1), (1,2) for store #1, (2,1), (2,2) for store #2 and so on). The corresponding methods on the ReceiptBean are getStoreNum() / setStoreNum() and getReceiptNum() / setReceiptNum().
Now, I want to define a bi-directional CMR between Store and Receipt, which means I need a CMR method getStore() for ReceiptBean, returning a StoreLocal.
Putting all this together, it seems the ReceiptBean must provide a getStore() and a getStoreNum() method, both of which are implemented by the container. Ditto for the setters. This seems horrible - there would be two columns in the underlying RDBMS table. Also, it would seem that the onus is on the bean provider to keep these two values in sync. In other words, if I create a new Receipt and in the ejbCreate() call setStoreNum(), I would also need to do a lookup of StoreLocalHome.findByPrimaryKey() to obtain the corresponding StoreLocal, and then call setStore().
The alternative approach would be to define the ReceiptPK to contain a StoreLocal reference, rather than a storeNumber value. Then, the methods would be the same and would match. This doesn't make the ReceiptPK object transportable by a remote client, however, so that feels wrong.
Any insights gratefully received...
Dan Haywood
-
EJB CMR & identifying primary keys (1 messages)
- Posted by: Dan Haywood
- Posted on: November 21 2001 07:52 EST
Threaded Messages (1)
- EJB CMR & identifying primary keys by Chengzhong Liao on November 25 2001 01:57 EST
-
EJB CMR & identifying primary keys[ Go to top ]
- Posted by: Chengzhong Liao
- Posted on: November 25 2001 01:57 EST
- in response to Dan Haywood
Putting all this together, it seems the ReceiptBean must >>provide a getStore() and a getStoreNum() method, both of >>which are implemented by the container. Ditto for the >>setters. This seems horrible - there would be two columns >>in the underlying RDBMS table. Also, it would seem that >>the onus is on the bean provider to keep these two values >>in sync. In other words, if I create a new Receipt and in >>the ejbCreate() call setStoreNum(), I would also need to >>do a lookup of StoreLocalHome.findByPrimaryKey() to >>obtain the corresponding StoreLocal, and then call >>setStore().
This is not true. You should use the cpm and cmr to map
the same column in the table. When you create a new
Receipt, you just call cmp setter, i.e.
setStoreNum(StoreNumType someNum) in ejbCreate().
Hope this helps,
Chengzhong