Hibernate Tutorial: What is difference between Hibernate 4 and 5?
Hibernate 5 is the next version of Hibernate after great success of Hibernate 4. Hibernate 4 was released back in 2012 with many new features and improvements over Hibernate 3. Now Hibernate 5 came with many new features, improvements and support for NoSQL database.
In this article we are disusing the new features added in this release of Hibernate and compare it with the previous version 4. If we just say Hibernate 5, then we are referring to the Hibernate 5 ORM framework which is used for writing the persistence layer for the application.
Hibernate 5 New features
First Let's see the new features added into Hibernate 5.
Support for Java 8 Date and Time
Hibernate 5 is the first version of Hibernate which fully supports the Java 8 Date/Time API. This version of Hibernate can use these Date/Time data types without explicit conversion in Java program. Developers can use them as any other supported types in the program and persistence will be handled well by the ORM system. For example if you use LocalDate type then Hibernate 5 ORM will perform its conversion into JDBC date type during persistence process.
Support for Stream query results
Hibernate 5 provides new method for getting the results as Stream, which allows the developers to efficiently implement usage cases where large quantity is to be retrieved. Internally it implements ScrollableResults for getting the data into Stream, where data is fetched into batches.
Load Object by Multiple IDs
There is new method byMultipleIds(), which is added to the session class and this is used to load the multiple objects at a time by providing the primary keys in multiLoad() function. This functionality is very useful when you know the primary keys and you want to load all at a time in list.
MultiIdentifierLoadAccess<Student> multi = session.byMultipleIds(Sudent.class); List<Student> student= multi.multiLoad(1L, 2L, 3L);
Above example shows you to load multiple entities at a time by providing the primary keys.
Joining unassociated entities in a query
Now hibernate supports the joining of unassociated entities in a query. For example you can use the INNER JOIN in the createQuery function. Hibernate will process the createQuery() and generate necessary JDBC query to fetch the results.
Support for @Repeatable annotations
This is another improvement which is now available with Hibernate 5. The @Repeatable annotations was introduced in Java 8, which actually used to annotate the same annotation multiple times. For example you can define multiple named queries under @NamedQueries annotation. Example:
@NamedQueries({ @NamedQuery(......), @NamedQuery(......) })
Check more at: What's new in Hibernate 5?
What is difference between Hibernate 4 and 5?
Here is the differences between Hibernate 4 and Hibernate 5.
Change in Lucene Query
Now while making sort you have to change SortField.STRING changed to SortField.Type.STRING. Here is example:
Old method:
fulltextQuery.setSort( new Sort( new SortField( "title", SortField.STRING ) ) );
Changed Method:
fulltextQuery.setSort( new Sort( new SortField( "title", SortField.Type.STRING ) ) );
Class re-names and re-locations
There are many classes which has been renamed to re-located to another package.
Here the the list of renamed or re-located classes:
Previous Location (Class) | New Location (Class) |
org.hibernate.search.engine.impl.SearchMappingBuilder | org.hibernate.search.engine.spi.SearchMappingBuilder |
org.hibernate.search.Environment | org.hibernate.search.cfg.Environment |
org.hibernate.search.FullTextFilter | org.hibernate.search.filter.FullTextFilter |
org.hibernate.search.indexes.impl.DirectoryBasedIndexManager | org.hibernate.search.indexes.spi.DirectoryBasedIndexManager |
org.hibernate.search.infinispan.impl.InfinispanDirectoryProvider | org.hibernate.search.infinispan.spi.InfinispanDirectoryProvider |
org.hibernate.search.ProjectionConstants | org.hibernate.search.engine.ProjectionConstants |
org.hibernate.search.SearchException | org.hibernate.search.exception.SearchException |
org.hibernate.search.spi.MassIndexerFactory | org.hibernate.search.batchindexing.spi.MassIndexerFactory |
org.hibernate.search.spi.SearchFactoryBuilder | org.hibernate.search.spi.SearchIntegratorBuilder |
org.hibernate.search.spi.SearchFactoryIntegrator | org.hibernate.search.spi.SearchIntegrator |
org.hibernate.search.Version | org.hibernate.search.engine.Version |
org.apache.lucene.queryParser.QueryParser | org.apache.lucene.queryparser.classic.QueryParser |
FullTextIndexEventListener now final
Now the FullTextIndexEventListener class is final class and can't be extended.
hibernate-search-analyzers module removed
The hibernate-search-analyzers module has also been removed from Hibernate 5.
Apart from all these changes more changes includes JMS controller API changed and ServiceRegistry API updated.
In this tutorial we have seen the differences between Hibernate 5 and Hibernate 4.
Explore following tutorials of Hibernate 5: