This is new in Grails 2.0, it's to support domain class reloading. Before 2.0 changing a domain class during run-app would cause the application to restart, but with the new reloading agent this isn't necessary. But since the persistence configuration may have changed, Grails rebuilds the SessionFactory
. But due to the way Hibernate is configured, other classes have a reference to the old one, so it cannot be replaced without knowing every class that has a reference. So the proxy is used to contain the replaceable real instance, and everyone calls methods on the proxy. When the SessionFactory
is replaced it's entirely internal to the proxy and other users don't know that anything happened.
This means that the SessionFactory
won't be a SessionFactoryImpl
, but the point of interfaces is to abstract away the concrete implementation class, so it's a bad practice to cast to what you expect the real class to be. This should work in older and current versions of Grails:
SessionFactory hibernateSessionFactory = (SessionFactory)sessionFactory;