[JIRA] (HHH-16943) Column ordering leads to wrong column order in unique constraints
by Benedikt Waldvogel (JIRA)
Benedikt Waldvogel ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiMTM1Y2ZlOGY1... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16943?atlOrigin=eyJpIjoiMTM1Y2... ) HHH-16943 ( https://hibernate.atlassian.net/browse/HHH-16943?atlOrigin=eyJpIjoiMTM1Y2... ) Column ordering leads to wrong column order in unique constraints ( https://hibernate.atlassian.net/browse/HHH-16943?atlOrigin=eyJpIjoiMTM1Y2... )
Change By: Benedikt Waldvogel ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
h3. Given
{code:java}@Entity
@Table(uniqueConstraints = @UniqueConstraint(name = "uk_b_a", columnNames = { "b", "a" }))
class TestEntity {
@Id
private Long id;
private String b;
private String a;
}{code}
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”.
h3. 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" (
id bigint not null,
a varchar(255),
b varchar(255),
primary key (id),
constraint uk_b_a unique (a, b)
);{code}
Note that “uk_b_a” is created with columns {color:#bf2600}*(a, b)*{color} instead of the expected column order *(b, a)*.
h3. Workaround
One can restore the behavior of Hibernate 6.1 by switching to the legacy column ordering strategy via {{hibernate.column_ordering_strategy=legacy}}.
h3. Test Case
_Upcoming [https : I’ll submit a PR with a unit test for this issue //github. _ com/hibernate/hibernate-orm/pull/7007|https://github.com/hibernate/hibern...]
( https://hibernate.atlassian.net/browse/HHH-16943#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16943#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100232- sha1:33a7c72 )