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.

Generating a Hibernate Mapping file with Hibernate Synchronizer in Eclipse

This is one way to generate an Hibernate Mapping file using the Hibernate Synchronizer plugin in Eclipse.

I assume you will want to put your mapping files in your WEB-INF/classes directory, so it will be picked up in the classpath.

1. Right Click the “classes” directory and choose New->Other->Hibernate Mapping File

Hibernate Mapping Screenshot

2. Next fill the appropriate settings; for example-

Hibernate Mapping Screenshot

And hit “Refresh”. If the information you entered is correct and the database connection succeeds, you will be presented with the option to choose which table you would like to generate a Hibernate mapping.

Hibernate Mapping Screenshot

Choose your package and the .hbm.xml file will be generated in your WEB-INF/classes.

Using Hibernate Synchronizer 3 with Spring and Hibernate 3

New: Update for Hibernate Synchronizer 3.1.9
Popular question on Hibernate Synchronizer forum involves using it with Spring. Here’s one way:

The following steps assume you have already installed the Hibernate Synchronizer in Eclipse:

1) Update the Hibernate Synchronizer to use a custom base root (Source Code)

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

2) Update the Hibernate Synchronizer to use a custom Exception class
Same location as above. Enter “org.springframework.dao.DataAccessException” for DAO Exception input box

3) Update Base DAO Snippets
a) On Eclipse Menu bar, click Window->Preferences
b) Click Hibernate Synchronizer on left panel
c) Click Snippets
d) Expand Base DAO
e) Replace Action Methods with this

f) Replace Finder Methods with this

g) Replace Imports with this
h) Replace Required Methods with this (I removed JRE 1.5 specific content from snippets)

4) 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:

<bean id=”usersDataAccessObject” class=”com.xyz.data.dao.UsersDAO”>
<property name=”sessionFactory”>
<ref local=”mySessionFactory”/>
</property>
</bean>

Of course, the above example assumes you have “mySessionFactory” bean defined.

Hope this helps. Comments welcome.

Using Hibernate Synchronizer 3.1.1 with Spring, Hibernate 2 and < JRE 1.5

The following steps assume you have already installed the Hibernate Synchronizer in Eclipse:

1) Update the Hibernate Synchronizer to use a custom base root (Source Code)

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

2) Update the Hibernate Synchronizer to use a custom Exception class
Same location as above. Enter “org.springframework.dao.DataAccessException” for DAO Exception input box

3) Update Base DAO Snippets
a) On Eclipse Menu bar, click Window->Preferences
b) Click Hibernate Synchronizer on left panel
c) Click Snippets
d) Expand Base DAO
e) Replace Action Methods with this

f) Replace Finder Methods with this
g) Replace Imports with this
h) Replace Required Methods with this

4) 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:

<bean id=”usersDataAccessObject” class=”com.xyz.data.dao.UsersDAO”>
<property name=”sessionFactory”>
<ref local=”mySessionFactory”/>
</property>
</bean>

Of course, the above example assumes you have “mySessionFactory” bean defined.

These changes are geared toward an environment still using Hiberate 2 and JRE/JDK 1.4 and below.

Also, I added this interceptor to the applicationContext.xml file:

<bean class=”org.springframework.orm.hibernate.HibernateInterceptor” id=”hibernateInterceptor”>
<bean id=”hibernateInterceptor” class=”org.springframework.orm.hibernate.HibernateInterceptor”>
<property name=”sessionFactory”>
<ref bean=”mySessionFactory”/>
</property>
</bean>

My gut tells me there is still some kinks to work out, but I’m up and running with it now. Hopefully, it helps us out. Let me know if you have ideas to improve it.

ServiceCycle is a registered trademark of Supergloo, inc..