Techniques For Integrating Hibernate Into Legacy Java Code Part 1

If you’re like me, you spend quite a few time facing legacy code that, for no matter what intent, does not take merit of revolutionary methodologies and libraries. I’ve taken over Java tasks that comprise lots of of hundreds of lines of code and no longer a unmarried 3rd-birthday party jar rather than a JDBC motive force! One of the so much time-honored examples of it truly is the implementation of the details get entry to layer. These days, the de facto technique involves Hibernate and DAOs, normally controlled by way of Spring.

This article will aspect the stairs I recently took to covert a sizeable software from tradition-written tips access to Hibernate and coupon websites in india Spring via the refactoring facilities in Eclipse. The key with this refactorization is to get the prevailing enterprise logic code (Struts Actions, JSPs, Delegate training, Business Service programs, and many others.) to get admission to the datastore utilizing Hibernate, managed by means of Spring, with out manually replacing any of that code in an instant. Part 1 will embody developing the Hibernate tips object training, DAOs, and refactoring the present code to paintings with these newly created varieties. Part 2 will conclude the venture with integration of the Hibernate DAOs and wiring every part up with Spring.

image

First of all, we need to create our Hibernate kind and DAO programs. Obviously, given that we’re coping with a legacy application and documents architecture, we will wish to use a bottom-up strategy to constructing our statistics entry layer. This just means that we’re going to generate the Java code and the best option Hibernate config info from the existing database. There are many tools freely reachable to make this system very painless. I propose an Eclipse Plugin for creating and asserting the Hibernate artifacts (Google Hibernate Eclipse Plugin to get commenced). The format and specifications for developing Hibernate training and config data are effectively documented some place else, so I gained’t pass into element right here. However, on this targeted mission, the Hibernate DAO lifecycles are managed by means of Spring, so the DAO categories have to all prolong HibernateDAOSupport.

Now we have java training (POJOs) which map to our database tables, however none of the present code uses those new records object sessions. This is the place the refactoring gear of Eclipse comes in tremendously at hand. For illustration, say we now have a legacy class known as AccountInfo which corresponds to the ACCOUNT database desk. Right-click on the type and make a choice Refactor -> Extract Interface. On the discussion container, call the recent interface IAccount and make certain you pick Use the extracted interface sort where you'll. Choose the alternative chances in accordance with your alternatives. Click OK and kick back while Eclipse transformations each and every occurence of AccountInfo references to IAccount references and recompiles. Of direction, try this with every one item form type.

image

If you in no way realized why OOP languages are so useful, you’re about to. Now we’re going to refactor the code so that all of the existing legacy could be hooked into the recent Hibernate sort periods instead of the legacy ones. Continuing with the AccountInfo instance, create a brand new magnificence you’ll maybe need to create a new package deal for this step referred to as Account that extends the Hibernate POJO for Account and implements the new IAccount interface.

image

This subsequent edge is the maximum time-eating, yet absolutely isn’t that negative. At this level, the newly created category will almost always involve a bunch of empty procedures containing best TODO feedback. This is due to the fact that the IAccount interface most possibly defies a number of tactics that will not be applied inside the Hibernate Account POJO. To treat those, we clearly prefer the recent Account class to delegate to its generated superclass whenever beneficial to satisfy its agreement as an IAccount form. As a actual international example from the utility I turned into running on, the legacy AccountInfo type explained a getter/setter pair for a belongings often called username, while the corresponding column within the ACCOUNT table used to be the fact is LOGIN_NAME. To sort out this, you possibly can effectively enforce the get/setUsername tricks in Account to delegate to get/setLoginName (from its superclass). I additionally needed to translate between countless knowledge types extremely just a little. For example, the legacy code could define many homes as Strings although the corresponding piece of data inside the database was once defined as an INT or TIMESTAMP. Again, do this with each and every object brand elegance.

To end up the statistics style layer, edit the perfect Hibernate and Spring configuration records to consult these new item form lessons. The application now has the skill to map database documents to Java items by using Hibernate, and the legacy code which refers to those periods has no longer required any modifying by using hand. To end up this refactorization mission, we desire to hook inside the Spring-supported Hibernate DAOs in a identical way. In Part 2 of this article, I will speak about refactoring the legacy code to study, write, and update records by using Hibernate and Spring.