4

If JPA does not depend on EJB and it has its own spec. Why do I need EJB ? What I cannot without EJB ?

I have read the following discussions but I really do not point out why it is still required ?

Why should we use EJB?

https://stackoverflow.com/questions/4464338/why-ejb-is-used-in-enterprise-applications

Community
  • 1
  • 1
Ahmet Karakaya
  • 9,899
  • 23
  • 86
  • 141
  • Read into 'Container Managed Transactions' and you'll start to see what EJBs are useful for. You don't NEED them, they can be a big help in certain application architectures. – Gimby Jun 23 '14 at 08:38
  • Does CMT work in JPA? – Ahmet Karakaya Jun 23 '14 at 08:40
  • 1
    You already know that JPA works inside EJBs, so I don't see why you would still have to ask this. – Gimby Jun 23 '14 at 08:43
  • as part of the EJB 3.0 specification (JSR 220) but can be used without and outside an EJB container. In Java EE 6, JPA 2.0 has its separate specification (JSR 317), outside the EJB 3.1 specification (JSR 318) – Ahmet Karakaya Jun 23 '14 at 08:46

3 Answers3

3

All of the above answers add important information to the question but misses on one critical point. The main selling point of EJB architecture was distributed component (apart from all other services such as container managed,transactional, secure etc.). The idea was to enable the business logic to run in distributed environment piggybacking on RMI/IIOP. Although over the years this architectural style proved bad. For distributed computing there were more successfull architecture based on webservices. Distributed components though were made look easy by EJB, had their own share of inherent complexities and performance woes and were later avoided whenever possible. An old but very interesting read on this matter by Martin Fowler can be read here where he is scathing in his attack on the lure of distributed architecture typically being promoted by the likes of EJB. Later people seems to have followed this wisdom and avoided the temptation to jump onto the "distributed architecture" bandwagon. In Java landscape, this was marked by the rise of Spring framework and Rod Johnson's famous book "Expert One-on-One J2EE Development without EJB". People and the likes of me have never missed EJB's since then :-)

P.S. To be fair to EJB specs and the guys working hard on it to improve, they certainly have made fair progress in the past decade and they are much modern and developer friendly. However their "distributed" nature has taken a back seat for good

Shailendra
  • 8,874
  • 2
  • 28
  • 37
  • I think building distributed components make EJB more important than other features which differs from other frameworks. – Ahmet Karakaya Jun 23 '14 at 11:15
  • 1
    http://manning.com/panda/panda_meapch1.pdf is great article that makes me understand some key points and EJB journey that brought us to current point. – Ahmet Karakaya Jun 23 '14 at 12:52
  • "1.1.4 Why choose EJB 3" – Ahmet Karakaya Jun 23 '14 at 13:27
  • +1 for distributed component. However IMHO it is yet another (important) EJB characteristic that used to be unique 15 years ago and became commonly available now. There are a lot of frameworks that help to distribute our building blocks, some of them much better than EJB. – AlexR Jun 23 '14 at 14:20
2

EJBs, or enterprise java beans are java classes that can be managed by Java EE container that guarantees services like

  • bean life cycle
  • thread management
  • transaction management etc

Yes, JPA is not a part of Java EE spec now. It moved to JSE. However in past when Entity Beans provided the "standard" bridge between java and relational databases world.

What you cannot do without EJB? I'd say nothing. I mean you can do everything without EJB. And the reason is that there are alternative solutions like Spring or Guice.

And as always you can write lower level code without any framework.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
AlexR
  • 114,158
  • 16
  • 130
  • 208
  • Java Persistence 2.1 is listed as a java ee 7 technologies in the link. http://www.oracle.com/technetwork/java/javaee/tech/index.html. When It becomes as a part of JSE and removed from Java EE tech? – Ahmet Karakaya Jun 23 '14 at 08:53
  • @mmcc18, thanks, good to know. I remember however that once upon a time it was a part of JSE – AlexR Jun 23 '14 at 09:07
  • It is not JavaSE (since then it would be part of the JDK, which it isn't). It can be *used with* JavaSE or JavaEE (and is bundled as part of JavaEE). – Neil Stockton Jun 23 '14 at 10:33
2

EJB is just a server side component that servers the request of the user. Each enterprise bean runs in a container which do some maintenance (transaction management, security management, bean life cycle etc.,) on behalf of the user/programmer. Thus it makes the developer to focus on the core job instead of reinventing all alone.

So we can conclude that EJB just reduces the amount of coding done by programmer and let the programmer to focus on the core business logic.

We can do the same without EJBs with some good amount of coding.

Thanks, JK

HJK
  • 1,382
  • 2
  • 9
  • 19