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.

25 Comments

  1. Thanks a lot. This blogspot helps me a lot - now the generated code form hibernate synchronizer is fully compatible with Spring live example ;-) .

    Comment by Solvina — March 25, 2006 @ 7:51 am

  2. I have tried using this but I now get the following error:
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: , no session or session was closed

    I had this working using just the code from the Hibernate Synchronizer and I wanted to get it to work with Spring and now I get this error.

    Thanks for any help

    Comment by Dennis — April 25, 2006 @ 6:52 am

  3. Dennis,

    This could be many things. Can you try taking out the “role” collection and testing? If is an issue with the “role” association, you should go a different way than if it is an issue with the Spring SessionFactory. Can you post up your Spring and Hibernate config files?

    Todd

    Comment by mcgrath — April 25, 2006 @ 11:50 am

  4. Dear Todd,

    I’d done the snippet upgrade and i got errors when classes were generated. There are several errors on generated DAO classes because it generates a constructor that passes a Session object to _RootDAO. I think that i need to delete
    public _BaseRootDAO (Session session) {setSession(session);} on BaseRootDAOClassConstructor. It’s a correct solution or have i a mistake on upgrade implementation?

    Another errors are:
    The type TestCasesDAO must implement the inherited abstract method TestCasesDAO.findAll()
    The type TestCasesDAO must implement the inherited abstract method TestCasesDAO.delete(TestCases)
    The type TestCasesDAO must implement the inherited abstract method TestCasesDAO.delete(Integer)

    Thank u in advance

    Regards,

    Jonatan

    Comment by Jonatan — June 22, 2006 @ 2:59 am

  5. Jonatan,

    I’m not in front of an example now, but I believe there should not be a _BaseRootDAO class. _RootDAO should extend SpringBaseRootDAO. SpringBaseRootDAO does not have a (Session session) constructor or findAll or delete methods.

    Please check to ensure your _RootDAO extends the provided SpringBaseRootDAO.

    Hope this helps.

    Comment by mcgrath — June 22, 2006 @ 6:16 am

  6. Hello. I tried this aswell and also found a few errors in my generated code. My BaseXDAO’s have a method looking something like this:

    public java.util.List loadAll()
    throws org.springframework.dao.DataAccessException {
    return loadAll(getReferenceClass());
    }

    But there is no method loadAll(Class aClass) anywhere to be found…
    I wonder how come?

    // Jonathan

    Comment by Jonathan — August 21, 2006 @ 6:03 am

  7. Hm how come we are not changing the class constructor methods to something else?
    The strange (Session session) konstruktor is clearly stated in the Base DAO snippet for class constructors…

    Comment by Jonathan — August 21, 2006 @ 6:57 am

  8. I am having a similar problem as Dennis with the lazy loading.
    I am using Spring 1.2.7 and Hibernate 3.0

    I don’t know what you mean by the “role” collection? and if I understand things correctly, I would have a hibernate.cfg.xml file as all the information is in the Spring file?

    I successfully get the “Applicaiton” for a name: code in the ApplicationDAO class:
    public Application get(String appName) throws Exception{

    Application app = new Application();
    app.setName(appName);

    List result = this.findByExample(app);

    if (result.size()>0){
    return (Application) result.get(0);
    }else{
    throw new Exception(”No Application found for Name=” + appName);
    }

    }

    But when I try to iterate over all the “IndividualItems” I get the error:
    Set iiapps = requestedApp.getIndividualItemApplications();

    for (Iterator iter = iiapps.iterator(); iter.hasNext();) {
    IndividualItemApplication individualItemAppliation = (IndividualItemApplication) iter.next();
    }

    My Spring applicationContext.xml is:

    com/weenergies/fo/structure/resource/Application.hbm.xml
    com/weenergies/fo/structure/resource/Attribute.hbm.xml
    com/weenergies/fo/structure/resource/IndividualItem.hbm.xml
    com/weenergies/fo/structure/resource/IndividualItemApplication.hbm.xml
    com/weenergies/fo/structure/resource/IndividualItemAttribute.hbm.xml
    com/weenergies/fo/structure/resource/IndividualItemType.hbm.xml
    com/weenergies/fo/structure/resource/LookupList.hbm.xml
    com/weenergies/fo/structure/resource/LookupListValues.hbm.xml

    org.hibernate.dialect.Oracle9Dialect

    false
    true
    org.hibernate.cache.EhCacheProvider

    and my hibernate hbm.xml files:
    Application.hbm.xml:

    IndividualItem.hbm.xml:

    IndividualItemApplication.hbm.xml:

    Comment by Tim K — August 23, 2006 @ 8:37 am

  9. I tried posting my spring applicationContext.xml and my hibernate hbm.xml files but id didn’t show up in my post.

    Any ideas?

    Comment by Tim K — August 23, 2006 @ 8:39 am

  10. Jonathan has reported problems when using new versions of Hibernate Sync.

    Please remove constructors for Base DAO->Class Constructors snippet in newer versions of Hibernate Synchronizer.

    Original post was created using version 3.1.1 version of Hibernate Synchronizer.

    I haven’t had a chance to update the instructions for new versions of Hibernate Synchronzier. Maybe someone already has?

    Comment by mcgrath — August 23, 2006 @ 8:55 am

  11. Tim, can you put the files on a public accessible web server?

    I was assuming “role” was the name of the dependent objects; e.g.

    <set name=”role” table=”WORKFLOWCOMMENTS” lazy=”false” cascade=”all” inverse=”true” order-by=”createtime desc”>
    <key column=”Workflowid”/>
    <one-to-many class=”com.udcpension.data.Workflowcomments” />
    </set>

    Comment by mcgrath — August 23, 2006 @ 8:59 am

  12. mcgrath:
    I have the whole eclipse project ziped up and located here:
    ftp://ftp.wepco.com/pub/DAK/Hib3SyncSpring1.2.7.zip

    Thanks for looking into this.

    Comment by Tim K — August 23, 2006 @ 10:31 am

  13. Tim,

    Got the files. If you’re getting a lazy initialization exception, try adjusting your set definitions; e.g.

    <set name=”IndividualItemApplications” inverse=”true” batch-size=”100″ lazy=”false” >
    <key column=”APPLICATION_ID”/>
    <one-to-many class=”IndividualItemApplication”/>
    </set>

    Also, you might want to look into the HibernateInterceptor in the Spring Framework.

    Hope this helps. For lazy loading of dependent collections, you might find the Hibernate Forums and/or SPring Forums a good place for info as well.

    Comment by mcgrath — August 23, 2006 @ 10:43 am

  14. mcgrath,

    I changed all the sets and many-to-one to have the lazy=”false” and it worked. My concern is that this will load the all the data from the database.

    I will look into the HibernateInterceptor and see what that does.

    Thanks for the response and the pointers for more information.

    Comment by Tim K — August 23, 2006 @ 10:59 am

  15. Tim, valid concern and if it were me, I’d try to find a way to avoid using lazy=false. I’m glad we figured out the problem.

    Another thing to look at is OpenSessionInViewInterceptor.

    Good luck!

    Comment by mcgrath — August 23, 2006 @ 11:15 am

  16. Okey I have removed the constructors taking a Session. Now there is still the problem that my DAO’s doesn’t implement the delete(X), delete(long), and findAll() methods which are defined in the DAO interface. What should I do about this?

    // Jonathan

    Comment by Jonathan — August 24, 2006 @ 3:26 am

  17. Jonathan,

    The newer versions of Hib Sync must have changed quite a bit if it is generating interfaces instead of classes for DAOs now. What version are you using?

    I can’t say for sure, since I’m using version 3.1.1 of Hib Sync, but the delete and findAll methods are available from HibernateTemplate which the SpringBaseRoot extends. So, if your DAOs inherit from SpringBaseRoot (via *BaseDAO via _RootDAO), I’d guess you could remove theses methods from the DAOs (and snippets as well).

    Todd

    Comment by mcgrath — August 24, 2006 @ 3:55 am

  18. Hm I am using version 3.1.9. I am starting to wonder if I won’t write the DAO’s manually instead. This seems trixy…

    Comment by Jonathan — August 25, 2006 @ 12:20 am

  19. Hi,

    I’ve just picked up this thread and i’m trying to use synchronizer 3.1.9, with Hibernate 3 and Spring (all of whiich I’m quite new to).

    I suppose my first question is - has anyone actually got this combination working successfully? If I follow through the various usggestions in this thread will I arrive at a workable solution?

    Now some questions about the actual instructions in the first post :
    1. Step 1 supplies source code SpringHibernate3BaseRootDAO.java to be used as the custom base root. This seems to be referred to as SpringBaseRootDAO thereafter - should I convert the code to use that name?
    2. Not quite sure how step 4 is achieved

    then later :
    >Jonathan has reported problems when using new versions of Hibernate Sync.

    >Please remove constructors for Base DAO->Class Constructors snippet in newer >versions of Hibernate Synchronizer.

    I presume this means I just remove the constructor snippets from for Base DAO

    Any help would be much appreciated in this area - perhaps if I can sort out exactly what needs to be done I’ll be able to write a new set of instructions (might be a case of the blind leading the blind though :-) )

    Comment by Malcolm — September 29, 2006 @ 12:51 am

  20. I also get the error about the loadAll method

    return loadAll(getReferenceClass());

    This method does not exist in the template or in the base class. Has anyone got a fix?

    Comment by mark — October 19, 2006 @ 3:47 am

  21. ok — i got the _ROOTDAO method inheriting from SpringHibernate3BaseRootDAO. I still have errors about the methods delete() and findall() as reported by Johnathan for my BASEDAO objects.

    From the text above I should not get these if I inhereit from SpringBaseRootDAO . I dont have this class. Maybe I did something wrong. But I cant see anywhere to create this SpringBaseRootDAO class.

    Comment by mark — October 22, 2006 @ 1:05 pm

  22. ok — i edited the the DAOClassDefinition snippit to exclude the text ” implements ${class.AbsoluteDAOInterfaceName}” I am not sure where the interface is generated as there does not appear to be any snippet for it that one can edit.

    Comment by mark — October 22, 2006 @ 1:19 pm

  23. Hi Mark,

    Thanks for your patience and help in this matter. As we have found out, these directions do not work with newer versions of Hibernate Synchronizer. I’m working on putting out a new tutorial.

    In the mean time and just to confirm- you can get the source code for SpringBaseRootDAO at:
    http://www.supergloo.com/blogfiles/SpringHibernate3BaseRootDAO.java

    -Todd

    Comment by mcgrath — October 23, 2006 @ 3:49 am

  24. Hello Todd,

    This is just a ping. I am having the same issue as Mark and Jonathan. There is still interest!

    James

    Comment by James — November 11, 2006 @ 8:22 pm

  25. Hi everybody,
    I updated the instructions for Hibernate Synchronizer 3.1.9

    http://www.supergloo.com/blog/index.php/2006/11/16/using-hibernate-synchronizer-319-with-spring-and-hibernate-3/

    Comment by mcgrath — November 16, 2006 @ 6:34 pm

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..