Browse DevX
Sign up for e-mail newsletters from DevX


Migrate Your J2EE Apps from EJB to Hibernate : Page 4

You've decided to switch from EJB to Hibernate for persistence in your J2EE applications, but you don't know what to do with your existing EJB tier. Learn how a few decisions upfront, some simple ground rules, and design patterns can smooth the transition.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

EJB Interactions
The good news is that Hibernate will participate in your container-managed transactions if you configure it to do so. It behaves exactly as you expect it to behave. You do not need to explicitly manage transactions to get it to observe your existing semantics or isolation levels. Durable (flushed) Hibernate updates will be committed when the container commits and rolled back when the container rolls back. Hibernate will defer transaction management to the container.

Caching Strategies
Caching is a complex subject that is too broad to discuss in depth here. In a nutshell, caching is a key to improving performance. Having an appropriate cache strategy and implementation can have a big impact on Hibernate performance. You should decide on your caching strategy upfront. Hibernate is a two-level cache architecture of which the first level is the Hibernate session. Hibernate allows you to plug in cache providers for the second-level cache. If you already have a caching solution in place you may want to consider using it as your cache provider.

Working with EJB and Hibernate Concurrently
You can use entity beans and Hibernate concurrently within an application. However, they are distinct mechanisms. An update made using one may not be visible to the other, as they typically don't share in-memory data. If you're mixing and matching bean and Hibernate operations for the same logical entity, remember that your Hibernate updated data will not be visible to your entity unless it's been flushed to the DBMS and vice-versa.

Your bean and Hibernate also must use the same primary key generation strategy. Otherwise, you could get duplicate keys. Hibernate provides several off-the-shelf generators. If they don't suit you, just write one that wraps your bean key-generation mechanism.

The Object-Centric Data Access You've Been Waiting For
A good design, some decisions upfront, careful refactoring, and testing are key to migrating successfully. Hibernate is exceptionally easy to use and well documented. And because it's open source, you can quickly dive into the source code to debug any problems that you encounter. Hibernate goes a long way towards fulfilling the ultimate EJB promise of scalable, portable, and efficient object-centric data access. Once you've made the leap towards lightweight architecture, you'll never look back. Next step: Spring!

For more detailed information on any of the issues mentioned in this article, go to Hibernate.org or read the recently published book by Christian Bauer and Gavin King, Hibernate In Action.

Lara D'Abreo is an independent consultant with over 10 years experience in commercial product development in the US, Japan, and the UK. She's currently based out of Sydney, Australia and spends her time trying to make J2EE systems run faster.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date