Hibernate and SQL Server with Unicode (UTF-8/UTF-16)

Hi, I want to setup Hibernate (6.6) and SQL Server (2019) with Unicode. In the Microsoft docs I found the information, that since SQL Server 2019 the VARCHAR type can be used with UTF-8, if the collation is set to UTF-8 (i.e. Latin1_General_100_CI_AS_SC_UTF8). The previous way is NVARCHAR (UTF-16).

What is the recommend way to use Unicode with Hibernate and SQL Server? And which configuration is needed in Hibernate? It does not matter whether the result is UFT-8 or UTF-16.

I also have a second case, with an already existing SQL Server database (Latin1_General_CI_AS) which should be converted to Unicode and should also use Hibernate. I have concerns about the primary keys, which are defined as UUID String:

@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
@Size(max = 36)
@Column(length = 36)
private String id;

In the database the id column is VARCHAR(36).

How to convert the database in a good way and how to handle the primary keys?

Hello @hibernategeist. I don’t believe the collection setting of your SQL Server instance would impact any configuration on the Hibernate side - it should be completely transparent and handled by the JDBC driver.

We cannot really help you with conversion of your database instance; from a quick google search, I found this post on Microsoft’s blog that mentions the topic https://techcommunity.microsoft.com/blog/sqlserver/introducing-utf-8-support-for-sql-server/734928. Regarding primary keys, again I don’t think any particular care would be needed.

This is an issue covered in the Short Guide:

See also the Javadoc for hibernate.use_nationalized_character_data:

1 Like