Column ordering leads to wrong column order in unique constraints
Description
is followed up by
relates to
Activity
Show:
Benedikt Waldvogel August 9, 2023 at 8:18 PM
When upgrading to Hibernate 6.2.7
, I noticed that the issue is fixed for unique constraints (thanks!) but it still happens for primary keys. I opened a new issue which is very similar to this one:
Given
Note that we define an explicit unique constraint on the columns (b, a) (in that very order). The column order could be important since the underlying unique index should be used to speed-up queries that select all entities for a given value of “b”.
Actual vs. Expected
When the schema gets created in the context of a JPA EntityManagerFactory, the columns are (re-)ordered during construction of the SessionFactoryImpl, Line 252: bootMetamodel.orderColumns( false )
This leads to the following DDL:
Note that “uk_b_a” is created with columns (a, b) instead of the expected column order (b, a).
Workaround
One can restore the behavior of Hibernate 6.1 by switching to the legacy column ordering strategy via
hibernate.column_ordering_strategy=legacy
.Test Case