Wednesday, January 17, 2007

EJB - Frequently Asked Questions(FAQs)

EJB - Frequently Asked Questions(FAQs)(Part - 2)


EJB - Java Interview Questions


11. How to implement an entity bean which the PrimaryKey is an autonumeric field?

The EJB 2 Spec (10.8.3 - Special case: Unknown primary key class) says that in cases where the PrimaryKeys are generated automatically by the underlying database, the bean provider must declare the findByPrimaryKey method to return java.lang.Object and specify the Primary Key Class as java.lang.Object in the Deployment Descriptor.

12. When defining the Primary Key for the Enterprise Bean, the Deployer using the Container Provider's tools will typically add additional container-managed fields to the concrete subclass of the entity bean class.

In this case, the Container must generate the Primary Key value when the entity bean instance is created (and before ejbPostCreate is invoked on the instance.)

13. What is clustering?

Clustering is grouping machines together to transparantly provide enterprise services. Clustering is an essential piece to solving the needs for today's large websites.

The client does not know the difference between approaching one server or approaching a cluster of servers.

14. Is it possible to share an HttpSession between a JSP and EJB?

What happens when I change a value in the HttpSession from inside an EJB? You can pass the HttpSession as parameter to an EJB method,only if all objects in session are serializable. This has to be consider as "passed-by-value", that means that it's read-only in the EJB. If anything is altered from inside the EJB, it won't be reflected back to the HttpSession of the Servlet Container.

15. If my session bean with single method insert record into 2 entity beans, how can know that the process is done in same transaction (the attributes for these beans are Required)?

If your session bean is using bean-managed transactions, you can ensure that the calls are handled in the same transaction by :

javax.transaction.UserTransaction tran= null;
try{
tran=ctx.getUserTransaction();
tran.begin();
myBeanHome1.create(....);
myBeanHome2.create(...);
tran.commit();
}catch(...){}
You may want to check if you're already running in a transaction by calling tran.getStatus().

16. When should I adopt BMP and when I should use CMP?

You can use CMP and BMP beans in the same application... obviously, a bean can be BMP or CMP, not both at the same time (they are mutually exclusive).

There is a common approach that is normally used and considered a good one. You should start developing CMP beans, unless you require some kind of special bean, like multi-tables, that cannot be completely realized with a single bean. Then, when you realize that you need something more or that you would prefer handling the persistence (performanbce issue are the most common reason), you can change the bean from a CMP to a BMP.

Source : www.javabeat.net


No comments: