Saturday, June 10, 2023
HomeJava8 issues it's essential know when migrating to Hibernate 6.x

8 issues it’s essential know when migrating to Hibernate 6.x

Hibernate 6 has been launched for some time, and the most recent Spring Information JPA model contains it as a dependency. So, it’s no shock that an increasing number of builders need to migrate their purposes to the most recent Hibernate model. As you’re studying this text, you’re most likely considered one of them. And also you may ask your self what it’s essential do emigrate your utility and what you get out of it. I summarized that on this article and included hyperlinks to additional articles that designate the completely different options in nice element.

As Steve Ebersole (Lead Developer Hibernate 6) defined when he joined us for an Skilled Session within the Persistence Hub, Hibernate 6.0 introduced us many inner enhancements. And for a second, it appeared like we wouldn’t get a lot out of it for our day-to-day tasks. However with variations 6.1 and 6.2, we begin to see the advantages of these modifications. Each releases included a bunch of fascinating enhancements and new options. So, in case you determine emigrate your utility, I like to recommend you skip model 6.0 and immediately migrate to the most recent model. And in case you determine to do this, listed here are 8 issues it is best to know.

1. Hibernate 6 is predicated on JPA 3, and that requires modifications

With the discharge of model 6.0.0.Remaining, the Hibernate workforce up to date the dependency to the Jakarta Persistence API (JPA) specification to model 3.

That’s not an sudden change, and because of the strict compatibility guidelines, updating the JPA specification normally doesn’t have an enormous impact on current purposes. However updating to JPA 3 is completely different.

As a part of the transition of the JPA specification from Oracle to the Eclipse Basis, the professional group needed to change all bundle and configuration parameter names from javax.persistence.* to jakarta.persistence.*. Meaning it’s essential change all import statements and your configuration recordsdata.

However don’t fear; it’s not as dangerous because it might sound. I supported a number of groups in migrating their purposes to Hibernate 6. You possibly can migrate your utility from JPA 2 to model 3 in only a few minutes by performing these easy steps:

  • Use the search and change command in your IDE or on the command line to switch javax.persistence with jakarta.persistence.
  • Construct your utility and run your take a look at suite to make sure all the pieces works as anticipated.

As I defined in my migration information, I like to recommend doing this earlier than migrating to Hibernate 6 by utilizing a Hibernate 5 model that helps JPA 3.

2. Hibernate’s proprietary Standards API obtained eliminated

One other change in Hibernate 6 that may require modifications to your current code base is the elimination of Hibernate’s proprietary Standards API.

In model 5, Hibernate supported its previous deprecated model of the Standards API and the Standards API outlined by the JPA specification. Each APIs do the identical, and it doesn’t make a lot sense to maintain supporting each of them. So, after the proprietary Standards API had been deprecated for a number of years, the Hibernate ORM workforce eliminated it in model 6.

That is dangerous information for you in case you at present see any deprecation warnings when working with a Standards API. As I defined in a earlier article, there isn’t a straightforward method to migrate your proprietary Standards API queries to JPA’s Standards API. You will have to reimplement every question utilizing the brand new API.

I’ve coached a number of groups throughout such a migration and defined my course of emigrate queries from Hibernate proprietary to JPA’s Standards API on this article: Migrating from Hibernate’s to JPA’s Standards API.

3. New naming technique for default sequences

One other small change it is best to find out about is Hibernate 6’s new default naming methods for database sequences.

This transformation impacts all entity lessons for which you modeled a numerical major key and informed Hibernate to make use of a database sequence to generate the worth with out specifying which sequence it shall use.

public class ChessPlayer {
    @GeneratedValue(technique = GenerationType.SEQUENCE)
    non-public Lengthy id;

As I clarify within the Hibernate Efficiency Tuning programs included within the Persistence Hub, utilizing a database sequence to generate major key values supplies the very best efficiency. It permits Hibernate to use a number of inner optimizations to cut back the variety of executed statements and delay their execution so long as doable. This supplies vital efficiency advantages in comparison with utilizing an autoincremented column.

When utilizing this strategy, you may both outline which sequence Hibernate shall use or use Hibernate’s default sequence. In earlier variations, Hibernate used the sequence hibernate_sequence because the default sequence for all entity lessons. Many builders had been involved about this strategy as a result of it created big gaps of their major key values, and so they frightened that they may hit the higher restrict of their knowledge kind.

When folks ask me about this, I all the time clarify that you just don’t want to fret about any of those considerations. Contemplating that the utmost worth of a Lengthy is 9,223,372,036,854,775,807, it’s reasonably unlikely that your utility will expend all obtainable values. And unbiased of the variety of sequences you’re utilizing, you may’t keep away from gaps in your major key values. If a transaction will get rolled again after you get a price from a sequence, that sequence worth is misplaced, and that creates a spot in your major key values.

However whether or not you are worried about these considerations or not, utilizing a separate database sequence for every entity class is a typical apply. And Hibernate 6’s new default naming technique makes that a little bit simpler. In case you don’t specify the sequence identify, Hibernate 6 provides the postfix _SEQ to the entity’s desk identify and makes use of that because the default sequence.

So, beginning with model 6, Hibernate makes use of the sequence ChessPlayer_SEQ when persisting above’s entity class.

16:15:04,917 DEBUG [org.hibernate.SQL] - 
16:15:04,947 DEBUG [org.hibernate.SQL] - 
        (birthDate, firstName, lastName, model, id) 
        (?, ?, ?, ?, ?)

If you wish to hold utilizing the previous default naming technique, it’s essential configure the ID_DB_STRUCTURE_NAMING_STRATEGY property in your persistence.xml file.

    <persistence-unit identify="my-persistence-unit">
            <property identify="" worth="normal" />

In case you set it to legacy, Hibernate will use the identical default technique as within the Hibernate variations >= 5.3 however <6. And in case you set it to single, you get the identical default technique as in Hibernate variations < 5.3.

I defined all of this in additional element in my article about Hibernate 6’s new sequence naming methods.

4. Incubating new options

The Hibernate workforce will use the brand new @Incubating annotation to warn customers {that a} new API or configuration parameter may nonetheless change. They need to use that in the event that they launch a characteristic for which they’re in search of additional person suggestions or in the event that they’re releasing a subset of a set of interconnected options. In that case, a few of the lacking options may require extra modifications on the already launched APIs. I listed a number of examples of incubating options in a current article.

Although this may trigger some inconveniences after we’re utilizing these new APIs, I feel this is usually a good strategy. It offers the Hibernate workforce extra flexibility and permits them to launch new options earlier.

However earlier than we come to a remaining conclusion about this strategy, we might want to wait and see how typically incubating options truly change. After they introduced this characteristic with the launch of Hibernate 6.0, the Hibernate groups mentioned they’d attempt to keep away from altering incubating APIs and options. So, I’m optimistic that this can solely hardly ever occur.

5. ResultTransformer obtained changed

When the ResultTransformer interface obtained deprecated in Hibernate 5 with out providing an alternate, many builders anticipated that the characteristic could be eliminated in a future model. However that’s not the case!

Hibernate 6 break up the ResultTransformer interface into the TupleTransformer and the ResultListTransformer interfaces. The aim of that change was to repair a design flaw within the previous ResultTransformer interface. It outlined 2 strategies:

  1. The transformTuple methodology, which transforms a single file, and
  2. The transformList methodology, which transforms your entire listing of the remodeled objects.
Question question = session.createNativeQuery("choose id as personId, first_name as firstName, last_name as lastName, metropolis from Particular person p")
    .setResultTransformer(new ResultTransformer(){
            public Object transformTuple(Object[] tuples, String[] aliases) {
                PersonDTO personDTO = new PersonDTO();
                return personDTO;
            public Checklist transformList(Checklist listing) {
                return listing;
Checklist<PersonDTO> listing = question.listing();

However virtually all implementations solely supplied a significant implementation for considered one of these strategies. So, the workforce determined to separate the two strategies and allow you to select which one you need to implement.

You possibly can see within the following code snippet that this simplifies the interfaces and allows you to use them as practical interfaces.

PersonDTO individual = (PersonDTO) session
        .createQuery("choose id as personId, first_name as firstName, last_name as lastName, metropolis from Particular person p", Object[].class)
        .setTupleTransformer((tuples, aliases) -> {
      "Remodel tuple");
                PersonDTO personDTO = new PersonDTO();
                return personDTO;

As I defined in my information to Hibernate’s ResultTransformers, the Hibernate workforce transformed all their transformer implementations to the brand new interfaces. So, the required modifications to your code base are minimal.

6. Mapping JSON paperwork obtained simpler

Hibernate 6 makes storing JSON paperwork in your database and mapping them to Java objects a lot simpler. In variations 4 and 5, you needed to implement a customized UserType that tells Hibernate tips on how to serialize and deserialize the JSON doc and tips on how to learn and write it to the database. Usually, all of that’s not vital with Hibernate 6.

Beginning with model 6, Hibernate can map a JSON doc to an @Embeddable. As I present within the Superior Hibernate course within the Persistence Hub, an embeddable is a reusable mapping part that turns into a part of the entity definition and will get mapped to the identical database desk.

You outline an embeddable by implementing a Java class and annotating it with @Embeddable. On this particular case, the embeddable represents the information construction to which you need to map the JSON doc.

public class MyJson implements Serializable {
    non-public String stringProp;
    non-public Lengthy longProp;


After you’ve outlined the embeddable, you need to use it as an entity attribute. That requires an extra @Embedded annotation on the attribute. It tells Hibernate to get additional mapping data from the embeddable class.

And if you wish to map the embeddable to a JSON doc, you additionally have to annotate it with @JdbcTypeCode(SqlTypes.JSON) and embrace a JSON mapping library in your classpath.

public class MyEntity {
    non-public Lengthy id;

    non-public MyJson jsonProperty;

Hibernate then serializes and deserializes the attribute to a JSON doc and shops it within the database. The kind of database column to which Hibernate maps the attribute is determined by your database dialect. In case you’re utilizing a PostgreSQL database, Hibernate makes use of a JSONB column.

And as I confirmed in a current episode of the Java persistence information, you need to use the identical strategy to map a JSON doc to a java.util.Map.

7. Java Data might be embeddables

For the reason that introduction of information, builders have needed to make use of them to mannequin immutable entity lessons. Sadly, that’s nonetheless not doable. However Hibernate 6 helps information as embeddables.

In variations 6.0 and 6.1, it’s essential implement an EmbeddableInstantiator for every file you need to use as an embeddable. The EmbeddableInstantiator is a proprietary Hibernate interface that tells Hibernate tips on how to instantiate an embeddable object. This removes JPA’s requirement of a no-argument constructor and allows you to mannequin an embeddable as a file.

Beginning with model 6.2, Hibernate supplies its personal EmbeddableInstantiator for all embeddable information.

Let’s check out a fast instance. You outline an embeddable file by annotating your file with an @Embeddable annotation. In case you’re utilizing Hibernate 6.0 or 6.1, you additionally have to annotate it with @EmbeddableInstantiator and reference your instantiator implementation.

public file Deal with (String road, String metropolis, String postalCode) {}

Implementing the EmbeddableInstantiator interface isn’t complicated and solely vital in case you’re utilizing Hibernate 6.0 or 6.1.

As you may see within the following code snippet, solely implementing the instantiate methodology requires a number of traces of code. Hibernate calls it each time it has to instantiate an embeddable object and supplies all of the attribute values in a ValueAccess object. That object comprises these values within the alphabetical order of the attributes’ names.

public class AddressInstantiator implements EmbeddableInstantiator {
    Logger log = LogManager.getLogger(this.getClass().getName());
    public boolean isInstance(Object object, SessionFactoryImplementor sessionFactory) {
        return object instanceof Deal with;
    public boolean isSameClass(Object object, SessionFactoryImplementor sessionFactory) {
        return object.getClass().equals( Deal with.class );
    public Object instantiate(ValueAccess valuesAccess, SessionFactoryImplementor sessionFactory) {
        // valuesAccess comprises attribute values in alphabetical order
        remaining String metropolis = valuesAccess.getValue(0, String.class);
        remaining String postalCode = valuesAccess.getValue(1, String.class);
        remaining String road = valuesAccess.getValue(2, String.class);"Instantiate Deal with embeddable for "+road+" "+postalCode+" "+metropolis);
        return new Deal with( road, metropolis, postalCode );

8. Improved OffsetDateTime and ZonedDateTime mapping

Hibernate 5 launched proprietary help for OffsetDateTime and ZondDateTime by changing them into the native timezone of your utility earlier than persisting the timestamp with out timezone data. When studying the timestamp from the database, Hibernate then added the native timezone to the timestamp.

That mapping works so long as all of your Java purposes use the identical timezone, the timezone by no means modifications and is a timezone with out daylight saving time. Most groups both averted this mapping or used UTC as their timezone.

Hibernate 6 improves this mapping by introducing the @TimeZoneStorage annotating and supporting 5 completely different mappings:

  • TimeZoneStorageType.NATIVE shops the timestamp in a column of kind TIMESTAMP_WITH_TIMEZONE,
  • TimeZoneStorageType.NORMALIZE makes use of the mapping launched in Hibernate 5. It normalizes the timestamp to your JDBC driver’s native timezone and persists it with out timezone data,
  • TimeZoneStorageType.NORMALIZE_UTC normalizes the timestamp to UTC and persists it with out timezone data,
  • TimeZoneStorageType.COLUMN shops the timestamp with out timezone data in a single column and the distinction between the supplied timezone and UTC in one other column,
  • TimeZoneStorageType.AUTO lets Hibernate select based mostly on the capabilities of your database. It both makes use of TimeZoneStorageType.NATIVE or TimeZoneStorageType.COLUMN.

I defined all these completely different mappings in nice element in my information to Hibernate 6’s improved ZonedDateTime and OffsetDateTime mapping.

Right here you may see a easy instance of a ZonedDateTime attribute that Hibernate shall map to a column of kind TIMESTAMP_WITH_TIMEZONE utilizing TimeZoneStorageType.NATIVE.

public class ChessGame {
    non-public ZonedDateTime zonedDateTime;


Migrating your utility to Hibernate 6 may require some work as a result of JPA 3 needed to change all bundle and configuration parameter names from javax.persistence to jakarta.persistence. The Hibernate workforce additionally eliminated some APIs which have been deprecated for a number of years.

However after you’ve accomplished the migration, you get entry to a number of new options and simplifications which might be definitely worth the effort. As I confirmed you within the article, mapping JSON paperwork and dealing with ZonedDateTime and OffsetDateTime obtained simpler and extra versatile. Implementing a TupleTransformer or ResultListTransformer can be simpler than the previous strategy. And with the brand new default naming technique for database sequences, the strategy utilized by most groups turns into the brand new default.

And in case you observe my weblog or the bulletins of the Hibernate workforce, you realize that that is just the start. There are various extra options and enhancements within the making from which you’ll profit after migrating your utility to Hibernate 6.

If you wish to keep updated with the most recent developments in Hibernate and Spring Information JPA, I like to recommend becoming a member of the Persistence Hub. Apart from many different issues, that provides you entry to my month-to-month Java Persistence Information during which I let you know concerning the newest releases and present you tips on how to use the brand new options.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments