1 year ago โ€ข Thorben Janssen

When migrating to Hibernate 6, you need to know the new implicit naming strategy for database sequences. It changes the default sequence Hibernate uses if you don't set a sequence name.
Here is what you need to do to switch it back or use your own naming strategy.

Set the configuration parameter hibernate.id.db_structure_naming_strategy in your persistence.xml to:
- standard (the default in Hibernate 6)
- legacy (used in Hibernate >=5.3)
- single (used in Hibernate <5.3)
- fully qualified class name of your implementation




    
        ...
        
            
            ...
        
    





๐—ต๐—ถ๐—ฏ๐—ฒ๐—ฟ๐—ป๐—ฎ๐˜๐—ฒ.๐—ถ๐—ฑ.๐—ฑ๐—ฏ_๐˜€๐˜๐—ฟ๐˜‚๐—ฐ๐˜๐˜‚๐—ฟ๐—ฒ_๐—ป๐—ฎ๐—บ๐—ถ๐—ป๐—ด_๐˜€๐˜๐—ฟ๐—ฎ๐˜๐—ฒ๐—ด๐˜† = ๐˜€๐˜๐—ฎ๐—ป๐—ฑ๐—ฎ๐—ฟ๐—ฑ
Hibernate 6 uses a separate database sequence for each entity class by default. The name of that sequence consists of the name of the database table to which the entity class gets mapped and the postfix _SEQ.



๐—ต๐—ถ๐—ฏ๐—ฒ๐—ฟ๐—ป๐—ฎ๐˜๐—ฒ.๐—ถ๐—ฑ.๐—ฑ๐—ฏ_๐˜€๐˜๐—ฟ๐˜‚๐—ฐ๐˜๐˜‚๐—ฟ๐—ฒ_๐—ป๐—ฎ๐—บ๐—ถ๐—ป๐—ด_๐˜€๐˜๐—ฟ๐—ฎ๐˜๐—ฒ๐—ด๐˜† = ๐—น๐—ฒ๐—ด๐—ฎ๐—ฐ๐˜†
This is the strategy used in versions >=5.3. Hibernate uses hibernate_sequence for primary key attributes annotated with @GeneratedValue that don't reference a generator or the name of the referenced generator



๐—ต๐—ถ๐—ฏ๐—ฒ๐—ฟ๐—ป๐—ฎ๐˜๐—ฒ.๐—ถ๐—ฑ.๐—ฑ๐—ฏ_๐˜€๐˜๐—ฟ๐˜‚๐—ฐ๐˜๐˜‚๐—ฟ๐—ฒ_๐—ป๐—ฎ๐—บ๐—ถ๐—ป๐—ด_๐˜€๐˜๐—ฟ๐—ฎ๐˜๐—ฒ๐—ด๐˜† = ๐˜€๐—ถ๐—ป๐—ด๐—น๐—ฒ
The naming strategy single is a simpler version of the legacy strategy and gets you the default naming of Hibernate in versions <5.3. It uses the sequence hibernate_sequence if you donโ€™t specify a sequence name.



๐—ต๐—ถ๐—ฏ๐—ฒ๐—ฟ๐—ป๐—ฎ๐˜๐—ฒ.๐—ถ๐—ฑ.๐—ฑ๐—ฏ_๐˜€๐˜๐—ฟ๐˜‚๐—ฐ๐˜๐˜‚๐—ฟ๐—ฒ_๐—ป๐—ฎ๐—บ๐—ถ๐—ป๐—ด_๐˜€๐˜๐—ฟ๐—ฎ๐˜๐—ฒ๐—ด๐˜† = ๐—ฐ๐—น๐—ฎ๐˜€๐˜€ ๐—ป๐—ฎ๐—บ๐—ฒ
You can use the same mechanism to provide your own naming strategy. You only need to provide an implementation of the ImplicitDatabaseObjectNamingStrategy interface and configure it in your persistence.xml




๐—Ÿ๐—ผ๐—ผ๐—ธ๐—ถ๐—ป๐—ด ๐—ณ๐—ผ๐—ฟ ๐—บ๐—ผ๐—ฟ๐—ฒ ๐—ฑ๐—ฒ๐˜๐—ฎ๐—ถ๐—น๐˜€?
Check out my post about the new implicit naming strategies for database sequences in Hibernate 6 
 https://thorben-janssen.com/sequence-naming-strategies-in-hibernate-6 

2 years ago (edited) โ€ข Thorben Janssen

In Java, a List is supposed to be an ordered Collection of elements. But that's not necessarily the case if you're persisting it with Hibernate. By default, it doesn't persist the order of the elements of an ElementCollection or to-many association modeled as a java.util.List.



Before Hibernate 6, you had to use an @OrderColumn annotation to tell Hibernate to store the position of each element of the to-many association or ElementCollection.



@Entity
public class ChessPlayer {
 
    @OneToMany(mappedBy = "playerWhite")
    @OrderColumn
    private Set gamesWhite;
 
    @OneToMany(mappedBy = "playerBlack")
    @OrderColumn(name="myOrderColumn")
    private Set gamesBlack;
     
    ...
}


Since Hibernate 6, you can define this globally for all ElementCollection and the owning sides of many-to-many associations by configuring the property hibernate.mapping.default_list_semantics.



    
        Hibernate example configuration - thorben-janssen.com
        false
 
        
            
             
            ...
        
    



When doing that, please keep in mind that Hibernate needs to update the position of all elements if you're adding or removing an element that's not the last one. This can require many SQL UPDATE statements and slow down your application.


I explain all of this in more detail in today's blog post:  https://thorben-janssen.com/hibernate ... 

2 years ago โ€ข Thorben Janssen

After my talk at the JavaLand conference, I was asked how to add Flyway to an application that's already deployed in production.
You can easily do that by implementing the following 2 steps:



๐—ฆ๐˜๐—ฒ๐—ฝ ๐Ÿญ:
Generate a DDL script that recreates your current database with all its schemas, tables, sequences, constraints, ...
Rename that script so that it follows Flyway's naming convention V__.sql and copy it to your db script folder.



๐—ฆ๐˜๐—ฒ๐—ฝ ๐Ÿฎ:
Baseline your existing database to create the flyway_schema_history table and store the first version record:

flyway -url=jdbc:postgresql://localhost:5432/codingChallenge-220404
       -user=postgres 
       -password=postgres 
       -baselineVersion=1 
       -baselineDescription=initial_version 
       baseline

This tells Flyway that your db is already in version 1 and prevents it from executing the 1st migration script.



๐—”๐—ป๐—ฑ ๐˜†๐—ผ๐˜‚'๐—ฟ๐—ฒ ๐—ฑ๐—ผ๐—ป๐—ฒ
Flyway will treat an existing database that contains the baseline version in the same way as a new database created using the 1st migration script.



I explained all of this in more detail at  https://thorben-janssen.com/flyway-migration-existing-database/ 

2 years ago โ€ข Thorben Janssen

Here is a new Hibernate 6 feature you should know: Improved ZonedDateTime & OffsetDateTime mappings



In version 5, the timestamp got normalized to the timezone of your JDBC driver or a configured timezone and stored without timezone info. Now you have multiple mapping options.



You can define your mapping by setting the hibernate.timezone.default_storage property in your configuration or by annotating your entity attribute with @TimeZoneStorage.



Hibernate 6 supports 5 TimeZoneStorageTypes:
๐—ง๐—ถ๐—บ๐—ฒ๐—ญ๐—ผ๐—ป๐—ฒ๐—ฆ๐˜๐—ผ๐—ฟ๐—ฎ๐—ด๐—ฒ๐—ง๐˜†๐—ฝ๐—ฒ๐˜€.๐—ก๐—”๐—ง๐—œ๐—ฉ๐—˜
Maps the ZonedDateTime or OffsetDateTime object to a column of type timestampe_with_timezone. 
This is the best mapping but only a few databases support the required column type.



๐—ง๐—ถ๐—บ๐—ฒ๐—ญ๐—ผ๐—ป๐—ฒ๐—ฆ๐˜๐—ผ๐—ฟ๐—ฎ๐—ด๐—ฒ๐—ง๐˜†๐—ฝ๐—ฒ.๐—ก๐—ข๐—ฅ๐— ๐—”๐—Ÿ๐—œ๐—ญ๐—˜
Normalizes the timestamp to the timezone of your JDBC driver or the one configured as hibernate.jdbc.time_zone and persists it without timezone info.
This is the same behavior as in  #Hibernate  5.
You should always configure a timezone without DST, e.g. UTC



๐—ง๐—ถ๐—บ๐—ฒ๐—ญ๐—ผ๐—ป๐—ฒ๐—ฆ๐˜๐—ผ๐—ฟ๐—ฎ๐—ด๐—ฒ๐—ง๐˜†๐—ฝ๐—ฒ.๐—ก๐—ข๐—ฅ๐— ๐—”๐—Ÿ๐—œ๐—ญ๐—˜_๐—จ๐—ง๐—–
Normalizes the timestamp to UTC and persists it without timezone information.
This is the better version of TimeZoneStorageType.NORMALIZE.
HHH-15174 prevents the conversion to UTC -> Same behavior as TimeZoneStorageType.NORMALIZE



๐—ง๐—ถ๐—บ๐—ฒ๐—ญ๐—ผ๐—ป๐—ฒ๐—ฆ๐˜๐—ผ๐—ฟ๐—ฎ๐—ด๐—ฒ๐—ง๐˜†๐—ฝ๐—ฒ.๐—–๐—ข๐—Ÿ๐—จ๐— ๐—ก
Persists the timestamp and the timezone offset in 2 separate columns. 
This is your best option if your database doesn't support the column type timezone_with_timestamp.



๐—ง๐—ถ๐—บ๐—ฒ๐—ญ๐—ผ๐—ป๐—ฒ๐—ฆ๐˜๐—ผ๐—ฟ๐—ฎ๐—ด๐—ฒ๐—ง๐˜†๐—ฝ๐—ฒ .๐—”๐—จ๐—ง๐—ข
Depending on your database's capabilities, Hibernate uses TimeZoneStorageType.NATIVE or TimeZoneStorageType.COLUMN.

I explained all mapping options in more detail in  https://thorben-janssen.com/hibernate-6-offsetdatetime-and-zoneddatetime/ 

2 years ago โ€ข Thorben Janssen

Even though JPQL is the most popular query language when using Hibernate, it's not the only one, and it's not the best fit for every use case.

JPQL's feature set is very limited. It only supports a small subset of the SQL standard and almost no database-specific features. And that's understandable. Because in the end, your database only understands SQL. So, why should JPA's expert group or the Hibernate development team try to design a fully-featured query language on top of SQL?


That doesn't make a lot of sense.


That's why JPA and Hibernate were designed as a leaky abstraction that lets the underlying SQL layer shine through its APIs. A perfect example of that is the support for native SQL queries.


Native queries enable you to use all query features supported by your database. You can define and execute them similarly to your JPQL queries. But there are a few things you should know about the mapping of your query result and a common performance pitfall you can easily avoid. I explain all of that in today's post:

 https://thorben-janssen.com/jpa-native-queries/ 

2 years ago โ€ข Thorben Janssen

If you've been following me for a while, you know that I have been offering courses online and offline for some time. Here are some things people said about them:
"We have included them in our standard training library" - Stephan Knitelius (Senior Consultant)
"Great combination of video lectures, example projects, and exercises" - Jean-Claude Brantschen (Software Engineer)
"Finally, I learned how to use criteria queries" - Gregor Karl Frey (Chief Development Architect at SAP)
"I have learned way more than I expected. Each topic explains the concept very well and helped me to understand it easily" - Tyler Karol

All my courses are now part of the Persistence Hub membership:  https://thorben-janssen.com/join-persistence-hub/ 

We have something to offer for every experience level. If you're a beginner, the following courses will provide you with all the knowledge you need to build your first persistence layer and to grow from there. And if you're already an experienced senior developer, these courses show you how to improve the performance of your persistence layer, implement complex technical requirements like multi-tenancy and manage the data and communication between your microservices.

Here are all certification courses currently included in the Persistence Hub:



JPA for Beginners
================

If you are new to JPA / Hibernate / Spring Data JPA, this course is for you.

You will learn all basic concepts, mapping annotations, and query features. After completing this course, you will be able to create simple persistence layers yourself and help your team to work on more complex projects.

โ€‹

Advanced Hibernate
==================
If you want to learn about JPA's and Hibernate's more advanced and complex mapping and query features, this course is for you.
You will learn how to use embeddables, composite primary keys, custom data types, inheritance mapping strategies, lifecycle callbacks, and common patterns to model your persistence layer. I will also show you how to use Hibernate's multi-tenancy feature, the Criteria API, and some of Hibernate's proprietary APIs to implement complex business requirements.


To get the most out of this course, you should be familiar with the basic concepts, mappings, and query features.

Hibernate Performance Tuning (Early Access - Release in December 2021)
=============================================================

This course is for you if you want to improve the performance of your persistence layer.
I will show you how to monitor Hibernate during development so that you can find performance problems as early as possible. You will learn how to use the best projection for each use case, avoid inefficient mappings, fetch associations as efficiently as possible, use Hibernate's caches, and much more.
To get the most out of this course, you should be familiar with Hibernate's basic concepts, mappings, and query features. You should also have built a few persistence layers with Hibernate and deployed them to production.

Spring Data JPA
==============
This course is for you if you're using Spring Data JPA to implement your persistence layer.
You will learn to configure Spring Data JPA, use its standard repositories and test your persistence layer. I will also show you how to avoid performance problems, extend Spring's repositories with and without implementing your own methods and give you an introduction to several integrations of other frameworks to provide auditing, create more complex queries, migrate your database, and much more.
To get the most out of this course, you should at least be familiar with JPA's basic concepts, mappings, and query features.

Data and Communication Patterns for Microservices
============================================
This course is for you if you want to design a microservice architecture for your application and wonder how these services will communicate. Unfortunately, that's not as easy as some conference speakers want to make you believe.
I will explain why most of the concepts used in a monolith no longer work in a microservice architecture. You will learn patterns to avoid distributed read operations, or if that's not possible, to at least make them as resilient as possible. I will also show you how to design distributed write operations that ensure data consistency without requiring any locks or causing other scalability issues.
To get the most out of this course, you should have a basic understanding of Spring Boot and some experience in system design.

All these courses are now part of the Persistence Hub. And even though this is a lot of material, it's only a part of the things included in the membership. You get access to Q&A calls and live Expert Sessions. All live events get recorded and uploaded to the Persistence Hub. You can also try your skills in Coding Challenges and be part of a community of developers who take their craft seriously.

Until December 10th, you can still become a founding member of this community and secure yourself a lifetime discount on your monthly or yearly payment:  https://thorben-janssen.com/join-persistence-hub/ 

2 years ago โ€ข Thorben Janssen

Busy day at the Persistence Hub:
We'll be hosting a Welcome Event for new members, I published the first Coding Challenge, and we have a new topic for the discussion of the week.

Now is the perfect time to join. You'll make it in time for all events, and you get a great discount
 https://thorben-janssen.com/join-persistence-hub 

2 years ago โ€ข Thorben Janssen

Join me tomorrow for the Persistence Hub Launch Event!
My membership helps you learn everything you need to build fast and maintainable persistence layers and get recognized for your technical expertise. The certification courses, live events, and community of like-minded developers are the quickest way to achieve your goals!

During the Launch Event, I will take you inside the membership, show you everything you get and answer your questions about my new membership. We will also talk about what you need to do to migrate your application to Hibernate 6, and I will, of course, answer your questions about JPA, Hibernate, and Spring Data JPA. 

Persistence Hub Launch Event

Thorben Janssen

Streamed 2 years ago โ€ข 1,317 views

2 years ago โ€ข Thorben Janssen

In today's blog, I'm answering an interesting question I got asked in a recent workshop. 
Someone wanted to mix multiple inheritance mapping strategies within 1 inheritance hierarchy. Hibernate doesn't support that but there is a different mapping that gets you almost the same result:  https://thorben-janssen.com/hibernate-mix-inheritance-mappings/?utm_source=social&utm_medium=youtube&utm_campaign=tutorial 

2 years ago โ€ข Thorben Janssen

Did you see yesterday's video?
This mapping might seem a little strange but a lot of applications need to use it to fulfill customer requirements. 

Modeling sequence-based composite primary keys with Hibernate

Thorben Janssen

2 years ago โ€ข 1,695 views