Search code examples
hibernatetapestry

Tapestry overriding HibernateSessionManager error


I am trying to override org.apache.tapestry5.hibernate.HibernateSessionManager. To do this I added this to my AppModule:

public static void contributeServiceOverride(
        MappedConfiguration<Class<?>, Object> configuration)
{
    configuration.addInstance(HibernateSessionManager.class,
            HibernateSessionManagerOverride.class);
}

The content of HibernateSessionManagerOverride (for the time being) is the same as the default Tapestry implementation org.apache.tapestry5.internal.hibernate.HibernateSessionManagerImpl.

When I run my application I get this error:

java.lang.RuntimeException: 
Exception constructing service 'ServletApplicationInitializer': 
Unable to instantiate class org.apache.tapestry5.services.TapestryModule as a module: 
Exception constructing service 'ServiceOverride': 
Error invoking service contribution method com.company.services.AppModule.contributeServiceOverride(MappedConfiguration): 
Exception constructing service 'ServiceOverride': 
Construction of service 'ServiceOverride' has failed due to recursion:
the service depends on itself in some way. Please check org.apache.tapestry5.ioc.internal.services.ServiceOverrideImpl(Map) (at ServiceOverrideImpl.java:31) via org.apache.tapestry5.ioc.services.TapestryIOCModule.bind(ServiceBinder) (at TapestryIOCModule.java:49) for references to another service that is itself dependent on service 'ServiceOverride'.

Solution

  • Looking at hibernateModule.java, I can see the following:

    @Contribute(ServiceOverride.class)
    public static void provideInjectableSessionObject(
            MappedConfiguration<Class, Object> configuration, 
            @HibernateCore Session session)
    {
        configuration.add(Session.class, session);
    }
    

    Perhaps this is causing the circular dependency?

    You might need to use a decorator instead of a ServiceOverride

    eg:

    public static HibernateSessionManager decorateHibernateSessionManager(
            HibernateSessionManager defaultManager, 
            @Autobuild HibernateSessionManagerOverride overrideManager)
    {
        return overrideManager;
    }