Just Curious about ejbPostCreate() method : ejbPostCreate() method is applicable only to entity beans. Can somebody tell me, what are the consequences if I try use it in my Session/Message Beans? I know, I shouldn't expect it to be called after the ejbCreate() method call but can I use it as a private method. Will I get any deployment or run time errors?
hi, Vish. I guess, you can add ejbPostCreate() method to SessionBean and MDB and compiler will not complain at all, but since this method is a callback method, it is intended for bean notification from EJB container, and EJB container does not suppose to call this method fo SB & MDB, this method will never be called (in a way it is called in Entity). Also, remeber about ejbPostCreate() as method in which you can setup relations for Entity EJBs, in this method you can use newly created bean's ID for relationship creating. This is primary (IMHO) purpose of this method. Cheers !!!
Java Platform, Enterprise Edition 6 Web Services Developer Certified Expert Exam Study Guide and Quiz Exam 1Z0-810: Upgrade to Java SE 8 Programmer Study Guide and Quiz
Joined: Aug 27, 2003
Thanks Mikalai, The method signatures of ejbPostCreate() method must correspond with the relevant ejbCreate() method, except for the return value which is primary key for ejbCreate() and void for ejbPostCreate() for entity beans. How about the list of application exceptions declared in the method signatures? I read somewhere that they are not required to match... Am I right?
How about the list of application exceptions declared in the method signatures? I read somewhere that they are not required to match...
You r right vish... however, while trying with some code, i came across a situation, which i a not able to u'stand. i have a bean Employee. its local home has a create method as - public Employee create(Integer id) throws CreateException, IOException; the bean class has a ejbCreate method as - public Integer ejbCreate(Integer EmpNo) throws IOException when i do ejbc , i get follwoing exception - [java] ERROR: Error from ejbc: [java] In EJB Employee, method create(java.lang.Integer) on the local home interface throws java.rmi.RemoteException. Methods on the local home interface must not throw java.rmi.RemoteException.
why? can't i throw any checked exception from create method? or does it have to be createException only?
Joined: Jun 04, 2002
Originally posted by Vish Kumar: How about the list of application exceptions declared in the method signatures? I read somewhere that they are not required to match... Am I right?
hi, Vish At least I did not read in spec that list of throwable exceptions for ejbCreate and ejbPostCreate has to be the SAME, so... I could guess that they may have declare different exceptions in their signatures... But how to hanle this situation... Client call "create()" method and as a result Container call ejbCreate + ejbPostCreate both methods on bean class. What happens if something bad happens in ejbCreate ? or ejbPostCreate ? What spec says about this (page 195):
All the exceptions defined in the throws clause of the matching ejbCreate<METHOD> and ejbPostCreate<METHOD> methods of the enterprise Bean class must be included in the throws clause of the matching create method of the home interface (i.e., the set of exceptions defined for the create method must be a superset of the union of exceptions defined for the ejbCreate<METHOD> and ejbPostCreate<METHOD> methods). The throws clause of a create<METHOD> method must include the javax.ejb.CreateException.
As we can see, ejbCreate and ejbPostCreate MAY have different list of application exceptions in throws definition, but all of them must present in "create()" declaration, because container would like to be prepared for something bad happened both in ejbCreate and ejbPostCreate. Some note about "create()" method... "create()" has to have : all exceptions declared in "ejbCreate()" + all exceptions declared in "ejbPostCreate()" + RemoteException (Remote view only) + CreateException (even if it is not declared for ejbCreate() or ejbPostCreate()) Think of "ejbCreate()" (or "ejbPostCreate()" ) as a BOSS (you see it has a CAPITAL letter in his name, so he IS a boss, and boss is free to declate CreateException or no; but "create()" - is a regular employee, it even has all it's name in lowercase, so he has no choice and it has to declare CreateException always). Cheers
Joined: Aug 27, 2003
Cheers Mikalai, for the good explanation...
By the way, I like your Exception hierarchy in your online book. That makes it easy for me to remember...