May 10, 2005

Hibernate Vs EJB 2.1(Entity Beans)

I need to add lot of meat to the skeleton which I have created in the previous post i.e. related to J2EE Architect worries. I have all the main sections created and I need to elaborate each one of them. It will take time and I need time to do that :).
I was planning to write on System Deisgn worries, but my boss urgently requested me to write something abt EJB and Hibernate , their differences based on my experience and doing some googling. This was urgently required for some proposal.
I finished this 2 pager and thought that I will share it with you all. It might be of some help.

Hibernate 3.0 & EJB 2.1 (CMP)

Hibernate is a powerful, ultra-high performance Object/Relational (OR) persistence and query service for Java.
Hibernate
  • integrates elegantly with all popular J2EE Application servers and Web containers with out any restrictions.
  • can also be used in stand alone Java applications.
  • supports and implements the EJB 3.0 (JSR 220) persistence standardization.

This comparison between Hibernate and EJB 2.1 is strictly with CMP Entity beans and not with EJB Platform per-say. EJB Platform (Session Beans/ MDBs) offer many advantages such as componentization, remote access to applications, variety of clients support (java/CORBA etc) and asynchronous message model.

The EJB persistence mechanism (CMP Entity beans) has many issues/disadvantages

  • they are heavy weight components
  • high runtime overhead
  • have a poor track record on the performance
  • cumbersome to develop, lot of interface definitions limiting developer productivity
  • Bean partitioning (Each bean a row in some table/Not every row of every table a Bean)
  • Vendor dependent CMP optimizations can help performance, at cost of portability
  • Inheritance not supported
  • Cannot be used for persistence in non-application server environments.
  • There is no dynamic query mechanism to lookup entity beans (finders are specified at compile time).
  • It is not easy to write unit tests for beans as it is not possible to use them outside of the application server.
  • No support for automatic primary key generation.Only relational databases are supported
The above list of disadvantages is the primary reason for the popularity of simple OR frameworks like Hibernate.

Hibernate Features/Advantages
Hibernate is open source product (similar to Struts and Log4j) and not vulnerable to any vendor lock-in and is supported by JBoss Inc.
  • Hibernate works on POJO principles and it is light weight
  • Hibernate is much more easier to use than handwritten SQL/JDBC (i.e. BMP Beans) and much, much easier to use and much powerful than Entity Beans 2.1
  • Hibernate always executes SQL statements using a JDBC PreparedStatement, which allows the database to cache the query plan.
  • Hibernate is able to implement certain optimizations (caching, outer join association fetching, JDBC batching, etc.) much more efficiently than typical handwritten JDBC.
  • You may use Hibernate from servlets or Struts actions, or from behind an EJB session bean facade. In a CMT environment, Hibernate integrates with the JTA Datasource and TransactionManager, as well as JNDI.
  • Hibernate has a very sophisticated second-level cache architecture and supports pluggable cache implementations.
  • Hibernate supports composite keys
  • Hibernate supports instance variables persistence i.e. Java Beans Style properties like getter/setters

Use-ful links:

www.hibernate.org/

www.google.com ;)

No comments: