Shall we define the set accessor method for the pk field in the entity bean class?
Spec (page 130) says:
Once the primary key for an entity bean has been set, the Bean Provider must not attempt to change it by use of set accessor methods on the primary key cmp-fields. The Bean Provider should therefore not expose the set accessor methods for the primary key cmp-fields in the component interface of the entity bean.
If this is the case, then how do we construct the primary key in the bean's ejbCreate method?
Well, first of all, you must provide an abstract getter/setter method pair in your bean class.
About the primary key, if it's not a String or a Wrapper, then you may create a compound class for it. This class must use a subset of the bean cmp field to make up the pk. Knowing this, yes, you must provide a getter and a setter for pk fields, as they are part of the bean cmp fields.
About the ejbCreate method now, this is the place where the bean is supposed to create its primary key. Out of this method, the pk must not be changed any more, but in the ejbCreate method, you MUST give value to the pk.
The component interface is not linked (well yes, but...you know what i mean) to the bean class. The cmp fields getters/setters are not supposed to be exposed through the component interface. But the bean class (and so the ejbCreate method) has a full access to these methods, so it may give a value to the PK fields as expected. [ April 20, 2006: Message edited by: Frederic Esnault ]
Assuming the simple case, the PK is a String. I think there are two things here:
1. When you say, you MUST set the PK in the ejbCreate, how do you do this? By calling the set method for the PK field? 2. The spec says, the PK setter method should not be exposed in the component interface. Now, if we're just writing an abstract setter for PK in the bean class, without exposing this setter in the component interface, is the container going to provide the implementation for this abstract setter method? If yes, how? If no, then how to set the PK in ejbCreate?
To brief you about how this came to my mind: I was just trying a sample EntityBean with J2EE RI. I exposed the setter for PK in the component interface (forgot what spec said). And in the bean class, provided the abstract setter method of the PK, and from ejbCreate called this abstract setter of the PK.
Now before deploying, when I tried to verify this, it gave me this message (most probably it was warning, not error) - that the setter method of the PK should not be exposed in the component interface.
1. Yes. A typical ejbCreate method will look like this (assuming PK is a string) :
Here the way a client sends parameters to the ejbCreate method (that may or may not be used to create the PK - for example a sequential PK will not use it) through the create method parameters.
And ONLY the ejbCreate method will be able to use them(or not) to call the cmp fields setters in the bean class.
The component interface must not show this setters to clients, and anyway why would you do this? The only way you can think of passing PK parameters is through the create method parameters. And keep in mind that the ONLY method responsible for PK creation is the ejbCreate() method, which has access to PK cmp fields setters.
2. Yes the container provides implementation for PK cmp fields, because PK is to be a subset of bean cmp fields. So the abstract setters for cmp fields selected to be part of PK receive (like the others) an implementation from the container. [ April 20, 2006: Message edited by: Frederic Esnault ]