Note: This issue is very similar to [https://hibernate.atlassian.net/browse/HHH-16943|https://hibernate.atlassian.net/browse/HHH-16943|smart-link] but the same problem happens for *composite primary keys*.
h2. Given
{code:java}@Entity @IdClass( CompositePrimaryKey.class ) static class TestEntity { @Id private String b; @Id private String a; }
private static class CompositePrimaryKey { private String b; private String a; // getters, setters, hashCode, equals }{code}
Note that we define the composite primary key on the columns *(b, a)* (in that very order). The column order could be important since the underlying primary key index should be used to speed-up queries that select all entities for a given value of “b”.
h2. 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 )|https://github.com/hibernate/hibernate-orm/blob/58aff00957721074a4a3937256edd64a067bc943/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java#L252]
This leads to the following DDL:
{code:sql}create table "TestEntity" ( a varchar(255) not null, b varchar(255) not null, primary key (a, b) ){code}
Note that the primary key is created for {color:#bf2600}*(a, b)*{color} instead of the expected column order *(b, a)*.
h2. Workaround
One can restore the behavior of Hibernate 6.1 by switching to the legacy column ordering strategy via {{hibernate.column_ordering_strategy=legacy}}.
h2. Test Case
_Upcoming…_ [https://github.com/hibernate/hibernate-orm/pull/7160|https://github.com/hibernate/hibernate-orm/pull/7160|smart-link] |
|