First page Back Continue Last page Graphics
...Entity Beans
Removal triggers ejbRemove( ) in bean class:
- Method of EntityBean interface
- Called just before removal
- Have it throw RemoveException to abort deletion
Removal of entity vs session beans:
- ejbRemove( ) is not the last EJB method of entity beans
- Use it to clean up entity data, not instance resources
See example in notes...
- In CustomerBean
- ejbRemove( ) prevents removal if customer has orders
Notes:
As with session beans, removing an entity bean triggers an ejbRemove( ) method in the bean. But there are some important differences here between session and entity beans:
This ejbRemove( ) method belongs to the EntityBean interface rather than SessionBean.
It throws an additional exception, RemoveException. Use that exception to prevent the entity from being removed (deleted).
ejbRemove( ) is not the last EJB method in the life of an entity bean as it is with a session bean. The entity is removed from the database, but the bean instance is simply returned to the pool and may be populated later with data for a different entity.
This last difference has important implications for how you use these methods. In a session bean, ejbRemove( ) is typically used to clean up instance resources, such as socket or database connections. In an entity bean, another method (discussed below) serves that purpose. Only use ejbRemove( ) to check and handle related entity data.
The example on the next page illustrates a simple but typical use of ejbRemove( )...