| Author |
Is this possible using CMP ? [keys from sequences]
|
Ryan Fernandes
Ranch Hand
Joined: Dec 11, 2003
Posts: 86
|
|
Hi, I know CMP is the recommended way to go when using EJB 2.0. However, I've got a question that I cant seem to figure out the answer to, Here's the scenario: 1. The Bean Provider creates a bean with 2 CMP fields. [ 1) ID, 2) Name ] 2. The Deployer has to map this bean to a particular table. However, the DBA has dictated that when anyone creates a row in this table, they have to first pull the ID from a sequence (we're using Oracle) and use that for the ID field. Would be absolutely grateful if anyone could tell me how this could be done (or if this is possible to do). If you say that the answer is vendor specific, please tell me how you would map a cmp-field that takes its new value from a Sequence in your EJB 2.0 container. Many thanks, Ryan
|
Unthinking respect for authority is the greatest enemy of truth. -Albert Einstein, physicist, Nobel laureate (1879-1955)
|
 |
Pradeep bhatt
Ranch Hand
Joined: Feb 27, 2002
Posts: 8876
|
|
Check out the BEA document http://edocs.bea.com/wls/docs81/ejb/entity.html#1155399 Could you please let me know which container are you using? :roll:
|
Groovy
|
 |
Kyle Brown
author
Ranch Hand
Joined: Aug 10, 2001
Posts: 3879
|
|
WebSphere for one does not allow sequence-generated keys like this. Kyle
|
Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
|
 |
Ryan Fernandes
Ranch Hand
Joined: Dec 11, 2003
Posts: 86
|
|
Thanks for your inputs guys. It got me thinking ( which is quite rare event just after the Holidays) How does this sound: How do entities get created anyway? By a client or a session facade or something of that sort that 'you' program. So I'm thinking, why not have a session bean that fires off an sql (no ejb-ql to help us here) that gets the next value from the sequence and pass that value in the create(key,xyz) of the entity. *The main thing is to do this in a single transaction.* Is there a better/simpler/vendor independant way to do this? Cheers, Ryan
|
 |
Kyle Brown
author
Ranch Hand
Joined: Aug 10, 2001
Posts: 3879
|
|
Actually that solution (doing it in a session facade through either custom SQL or an entity bean that only wraps a sequence table) is probably the most vendor-independent solution. We actually discuss doing that in my new book. Kyle [ January 04, 2004: Message edited by: Kyle Brown ]
|
 |
David Harkness
Ranch Hand
Joined: Aug 07, 2003
Posts: 1646
|
|
Originally posted by Ryan Fernandes: So I'm thinking, why not have a session bean that fires off an sql (no ejb-ql to help us here) that gets the next value from the sequence and pass that value in the create(key,xyz) of the entity.
As Kyle stated, this is a good pattern. You may end up switching containers from WebLogic (that has this facility) to WebSphere (which apparently doesn't, which is surprising), and then you'd be stuck implementing it under the gun. Of course, how many times have you switched databases or J2EE containers? Yeah, same here, yet I continue to code as if we might just do it tomorrow.
Originally posted by Ryan Fernandes: *The main thing is to do this in a single transaction.*
Since you're using Oracle sequences, this is irrelevant. Retrieving the next number from an Oracle sequence is an atomic operation that occurs outside the transaction -- rolling back will *not* reuse the same sequence number. This is done to avoid nasty locks against a sequence from several transactions when all you're doing is trying not to "waste" a meaningless number. If you need sequences that can be rolled back, you'll need to roll your own.
|
 |
Ryan Fernandes
Ranch Hand
Joined: Dec 11, 2003
Posts: 86
|
|
Thanks Kyle and David, Yup, after coming to Java I've got it driven into my brain that I need to write code that could run anywhere. And David, you're correct about the sequence bit. Thanks mate. Cheers, Ryan
|
 |
Sergiu Truta
Ranch Hand
Joined: Dec 16, 2003
Posts: 121
|
|
|
I have a suggestion...instead of using a sequence and access the database each time you need a new id(I see this as a performance killer) you could use the Sequence Blocks pattern for ID generation. Check this out in Floyd Marinescu's "EJB Design Patterns". You can find it on www.theserverside.com
|
...watch me...as I'm walking the path...
|
 |
 |
|
|
subject: Is this possible using CMP ? [keys from sequences]
|
|
|