File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes Generic CRUD service bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Generic CRUD service bean" Watch "Generic CRUD service bean" New topic

Generic CRUD service bean

Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Howdy Ranchers!

I'm a beginner in EJB world, so I will be glad for your advice how to solve this problem. Thanks in advance!

I use many business objects in my application, where a great vast of them use the basic CRUD operations. I'm looking for some unified way to implement those basic features.

In other words, there is a solution proposed by Adam Bien (

The generic interface is fine, but I still need to implement basic operations for each of my Business Objects.
The second solution (bean implementation) is great, but... how should I use it?

1. Should I use composition and inject this CrudServiceBean into mine business object and perhaps delegate all CRUD operations to this EJB, somewhat like this:

In this way I still need to write every CRUD method in my business object class (just a delegation but still in 90% it's just the same plain delegation code).

2. I can extend the CrudServiceBean like the code below:

Can I extend another EJB?
If I cannot, than this CrudServiceBean must be a POJO and therefore it will not have EntityManager injected. This sucks pretty badly...

3. I can extend the CrudServiceBean (plain POJO) and modify it to use a method like getEntityManager() within:

However, in this solution the getEntityManager() method must be at least package-visible (because of the overriding method from business object). I am not sure if this is the correct approach, as it exposes the EntityManager for other BusinessObjects...

Which approach would you choose and what is the rationale?

Thanks in advance!

I agree. Here's the link:
subject: Generic CRUD service bean
It's not a secret anymore!