views:

388

answers:

2

Hi,

I have a SEAM app with some JPA/Hibernate entities. And I now wonder where to put my query, persistence methods.

The default choice seems to put them in a session bean layer with injected

@PersistenceContext(...) @Inject EntityManager entityManager;

But I think I would rather have the methods on the entities themselves. What are the pro's/con's?

  1. Testablity?
  2. Niceness?
  3. Performance?

And does anyone have links to best practices for interacting with the EntityManager etc. from methods on the Entities rather than session beans?

Best, Anders

+1  A: 

I have no experience with SEAM, but from my experience with Java projects, I found it easiest to keep beans clear of persist methods. What we usually do:

  • Have beans for business objects (like "User" and "Setting" for example)
  • Have a DAO layer which can persist and retrieve these beans (simple CRUD)
  • Have a Service Layer which nows how to handle the beans, and maybe even how to build an aggregate of beans

This way, everything is pretty separated, and is easy to unittest. Performance is usually not an issue with this setup.

Rolf
+1  A: 

Yes, that is also what I have done before.

In general, I think, EJB is insanely verbose and boilerplate'y, but SEAM actually helps a bit, so that is why, in my current project, the extra layer of session beans just to query and persist annoys me. I have the feeling that I could make a reasonably concise app if I could kill this layer...

anders.norgaard