Hibernate 4.x example for TRIMM JPA

Hibernate 4.x example for TRIMM JPA

We have just added an example of how to use TRIMM JPA to generate code for Hibernate 4.x (in this case 4.2.4) to our TRIMM-Examples repository. The project is called TrimmHibernate4xMDDExample.

If you are new to TRIMM JPA, check out this tutorial on how to use it

The largest difference from the Hibernate 3.6.x example, called MDDExample, are the following:

  • To Persist DateTime and LocalDate (from JODA time) – we now use org.jadira.usertype.usertype.core and the latest joda-time version.
    This means that we have removed the dependency to joda-time-hibernate.
  • We have therefore added the following Maven jar dependency:

           <dependency>
                <groupId>joda-time</groupId>
                <artifactId>joda-time</artifactId>
                <version>2.2</version>
            </dependency>
            <dependency>
                <groupId>org.jadira.usertype</groupId>
                <artifactId>usertype.core</artifactId>
                <version>3.1.0.CR8</version>
            </dependency>
          

  • To persist using org.jadira.usertype.usertype.corewe changed the jpaTypeConverterClass to the following (in /model/JpaModel.yml):
    mapUmlPropertyTypes:
        - name: DateTime
          javaType: org.joda.time.DateTime
          jpaTypeConverterClass: org.jadira.usertype.dateandtime.joda.PersistentDateTime
        - name: LocalDate
          javaType: org.joda.time.LocalDate
          jpaTypeConverterClass: org.jadira.usertype.dateandtime.joda.PersistentLocalDate
    
  • To create Blobs and Clobs Hibernate 4.1 and larger now requires access to the active Hibernate Session. Since our CompleteModelIntegrationTest base class (dk.tigerteam.mddexample.model.BaseJpaModelTest) uses Spring to configure the EntityManagerFactory and control JPA transactions we could use a little Spring trick to get the current transactions EntityManager and thereby Session:
        private Session getHibernateSession() {
            EntityManagerHolder emHolder = (EntityManagerHolder)TransactionSynchronizationManager.getResource(entityManagerFactory);
            return (Session) emHolder.getEntityManager().getDelegate();
        }
    

    With this we can now do the following to create testdata for Clobs and Blobs:

    return Hibernate.getLobCreator(getHibernateSession()).createClob(...);
    

Give it a go and let us know if there are any problem using TRIMM for generating Hibernate 4 code 🙂

 
Comments

No comments yet.