James Williams
LinkedInMastodonGithub

Hibernate and JPA with Griffon

Whilst tradional GORM is forthcoming and there is a preview release of the MongoDB plugin with some GORM-like features, there might be a reason why you might need to use Hibernate but not GORM. You might want to slowly evolve an application and work inward from the Swing bits. Using Guice and the warp-persist extension, you can quickly wire Hibernate support into a Griffon application.

I started with the warp-persist-samples DAO example located here and copied all the source files to the Griffon app. It demonstrates normal pre-GORM data persistence patterns with a data access object for each data type.

I put the persistence.xml file into the resources/META-INF directory of the Griffon application and the following in my controller to instantiate the Guice injector and connections to the database (after creating the projectClient object in the model:

model.projectClient = Guice.createInjector(
     new MainModule(),
     PersistenceService.usingJpa().across(UnitOfWork.TRANSACTION)
         .transactedWith(TransactionStrategy.LOCAL).forAll(
            Matchers.any()).buildModule()
).getInstance(TestClient.class);
model.projectClient.go();

warp-persist uses guice 1.0 and the griffon plugin uses 2.0 so the plugin isn't needed to enable Hibernate support. While the resulting application lacks GUI interaction, it shows how quickly basic Hibernate support can be added and why that is no longer a barrier to Griffon adoption.