Using Hibernate Synchronizer 3.1.9 with Spring and Hibernate 3

The following will teach you how to use the latest version of Hibernate Synchronizer with the Spring Framework. This post assumes you already installed the Hibernate Synchronizer in Eclipse.

Before, I begin, it’s obvious Joe is working on Spring Integration in Hibernate Synchronizer. I missed a few releases from 3.1 to 3.1.9, but there is a Spring Template now included. I’m not sure how to use it.

Like many things, there are more than one way to do things. The following describes how I have integrated Hibernate Synchronizer with Spring Framework and Hibernate 3. I hope it is helpful to you. You may also wish to see my previous posts on Hibernate Synchronizer and Spring

Overview:
Section 1: Update Hibernate Synchronizer settings for a particular project
Section 2: Update Hibernate Synchronizer global settings for all projects
Section 3: Generate and Configure your Hibernate mapping files
Section 4: Delete the Interfaces
Section 5: Configure Your Spring Files


Section 1: Update HibernateSynchronizer settings for a particular project
1. Update the Hibernate Synchronizer to use a custom base root. From Menu bar: Project->Properties->Hibernate Synchronizer->Data Access Objects tab->Check “I would like to use a custom root” and enter full path to the SpringBaseRootDAO; e.g. com.codesponsors.data.dao.SpringBaseRootDAO. Click Here for SpringBaseRootDAO source2. Update the Hibernate Synchronizer to use a custom Exception class. Same location as above. Enter “org.springframework.dao.DataAccessException” for DAO Exception input box

Section 2: Update HibernateSynchronizer global settings for all projects

1. On Eclipse Menu bar, click Window->Preferences

2. Click Hibernate Synchronizer on left panel

3. Click Snippets

4. Update Base DAO Snippets

a) Expand Base DAO
b) Replace Action Methods with this content
c) Replace Finder Methods with this content
d) Replace Imports with this:

import ${class.DAOPackage}.${class.DAOClassName};

e) Replace Required Methods with this content (I removed JRE 1.5 specific content from snippets)
f) Replace Class Constructors with:

public ${class.BaseDAOClassName} () {}

5) Update Root DAO Snippets
a) Expand Root DAO
b) Replace Constructor Methods with this:

public _RootDAO () {}

6) Update Base Root DAO Snippets
a) Expand Base Root DAO
b) Replace Constructor Methods with this:

public _BaseRootDAO () {}

7) Update DAO Snippets
a) Expand DAO
b) Replace Class Definition with this:

public class ${class.DAOClassName} extends ${class.BaseDAOClassName} {

c) Replace Class Constructors with:

public ${class.DAOClassName} () {}

Section 3: Generate and Configure your Hibernate mapping files

1) In your classes directory, generate your Hibernate Mapping file. See post: Generating a Hibernate Mapping file with Hibernate Synchronizer in Eclipse for more info.

2) Open the Hibernate Mapping file and change the meta sync-DAO attribute from false to true

3) Right click on the Hibernate Mapping file, choose Hibernate Synchronizer and select Synchronize Files. You should now have plenty of new classes and packages in your source folder.

Section 4: Delete the Interfaces

Delete the interface files generated. For example, if your base package is com.codesponsors.data, then delete the com.codesponsors.data.dao.iface package. Yes, I meant to say that, delete the interfaces.
Now, I realize some senior developers and architects will want to talk about best practices in OO programming. We will talk about the benefits of providing the API in an interface form vs. an implementation class. We will discuss how this will allow implementation to change in the future and not have to track down all the changes in the calling code. We might discuss code readability. Yes, most of the time I agree. In fact, I use interfaces all the time for these same reasons. However, I think it’s overkill in DAO pattern and especially when using Hibernate. In the concept sense, Hibernate is the implementation while the DAO classes are the interface. This probably warrants another post entirely, so I’ll cut it short. I’ve been using Hibernate Synchronizer with Spring for over a year. See my previous posts. The introduction of Interfaces in the last releases of Hibernate Synchronizer offer no benefit to my current or future applications. Please leave comments to convince me otherwise. I look forward to your insights. Anyhow, moving on…

Section 5: Configure Your Spring Files

1) Configure your generated DAO(s) to inject a LocalSessionFactory for HibernateTemplate’s “sessionFactory” property (SpringBaseRootDAO extends HibernateTemplate). For example, an entry in the applicationContext.xml might look like this content

Conclusion

Hope this helps! As I said, comments are welcome and appreciated.

PS - I wish we could provide a simplified version of Hibernate Synchronizer specifically for Spring Framework users. It seems like Joe is on his way to provide this functionality, but I’m not sure which way he is heading.

PSS- I’m thinking of making the above instructions into a screencast. Would that be helpful? I would have to charge a little for the bandwidth and effort to make it. Maybe $7? Contact Me or leave comments if you are interested in a screencast of these instructions.

4 Comments

  1. Hi, I not found che Class Constructors of any DAO, can you help me?

    Comment by Domenico — November 25, 2006 @ 10:02 am

  2. Thanks for writing such a helpful article! I think it is a very good start for getting people going with Spring/HibernateSynchronizer. I have to point out though that deleting the interfaces is a mistake :)

    If you use the interfaces it will be much easier to switch out the form of backing for your DAO. You may be in the mind set that you only use hibernate/database behind the DAO. But what if you need to change your DAO implementation being used? What if the CIO of your company demands it? Maybe it will be backed by a SOAP service or messaging system? If you only have a concrete class. The class will be strongly coupled into all the code you wrote that used the DAO class.

    With Spring it is especially easy to switch out one DAO implementation with another (if you use interfaces!). I’m guessing you don’t really have much need for reusing the database code generated by Synchronizer?

    Just my 2 cents. Thanks for the good article.

    Comment by Rob Ottaway — December 3, 2006 @ 10:27 am

  3. Hi, glad to read your artical.But I have a question that how to generate the spring config file automatically.It seems no mention about it.Just edit by hand?Will you please help me?

    Comment by Rubbin — December 10, 2006 @ 10:40 pm

  4. Again about the interfaces. One of the top Spring guideline is easy testing eith dependency injection. I think it’s enough to use them.

    Personally I use interfaces to mock my daos while unit testing business logic layer.

    Good article, good work :)

    Comment by dPedro — December 18, 2006 @ 1:03 am

RSS feed for comments on this post. TrackBack URI

Sorry, the comment form is closed at this time.

ServiceCycle is a registered trademark of Supergloo, inc..