4

I am designing a web-application that access many database tables. I am trying to figure out what is the preferred way to access those tables? Is it via JPA or EJB?

Thanks, Nathan

Nathan
  • 41
  • 1
  • 3

2 Answers2

12

The answer is 'both'.

EJB itself doesn't access any DB tables. Everything you do in Java that relates to the DB happens via the Java Persistence API (JPA), or if you want to do low level stuff via JDBC but let's not get into that here.

What EJB brings to the table is a very easy management of transactions. You always need those with JPA, and it's a bit of pain to manage these manually. EJB also gives you very easy access to main class that you will use in JPA to interact with the DB: the entity manager.

Using EJB in practice is for a lot of simple and lightweight situations nothing more than adding the @Stateless annotation to a bean:

@Stateless
public class FooService {

    @PersistenceContext
    private EntityManager entityManager;

    public Foo getByID(Long fooID) {
        return entityManager.find(Foo.class, ID);
    }
}

Without EJB, the code to do this simple find would be much more verbose. And without JPA, there simply wouldn't be any code. As said before, EJB has no functionality to access the DB.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
0

Unless your actually building an enterprise system and have a need for the added complexity of EJB, just use JPA. It sounds to me like your just building a web app and you need simple db access - go for JPA. We use OpenJPA, and have no issues with it.

Joel
  • 29,538
  • 35
  • 110
  • 138
  • 3
    I don't really agree with that. EJB doesn't add complexity, it takes away complexity. In any web profile compliant server, you just add a @Stateless annotation to a bean and that's it. Using JPA manually you have to surround every operation with try catch finally, start of the transaction do checks, and commit or rollback and close the session. That's a lot of complexity in my book, compared to just adding a single @Stateless annotation. The most basic web application greatly benefits from EJB. That's why it's part of the Web Profile and implemented by super light-weight servers like Resin. – Arjan Tijms Jan 09 '11 at 17:26
  • It absolutely adds complexity if you're using it for a simple app that doesn't need the solutions to the problems it solves, and I would suggest that using EJB just to manage a simple transaction is excessive. In a simple app that logic could be handled equally well by some simple code in a ServletFilter, or just manually. – Joel Jan 09 '11 at 18:52
  • 1
    I still don't agree. There is nothing excessive about adding the @Stateless annotation to a bean. Manually handling transactions the low-level way is more error prone, more verbose, more complex and makes the code harder to read and thus maintain. These days EJBs are extremely light-weight. You might remember the old EJB 2 beans, which would indeed have been excessive. But not EJB 3 and certainly not EJB3.1 lite. Especially the latter are specifically meant for really simple apps. They don't require any xml or interface and can be put directly in an arbitrary package in a plain war. – Arjan Tijms Jan 09 '11 at 20:30
  • I haven't yet used EJB3, this is true, although I'm always wary of pulling in yet another API to solve a simple problem. We'll have to agree to disagree on that one! I'd definitely advocate using JPA over raw JDBC in some form or other though. The big plus that I found was no longer having to maintain sql schema files, but instead generating he schema directly from my entities. – Joel Jan 09 '11 at 21:08