[hibernate-issues] [JIRA] (HHH-14105) JoinColumn for composite key requires alphabetical order

Mikhail Mayorov (JIRA) jira at hibernate.atlassian.net
Tue Jul 14 01:53:00 EDT 2020


Mikhail Mayorov ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5cd931e24401890dcab39b3a ) *updated* an issue

Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNmMyNzhhMzIwZWI3NGE0NzgyM2ExZThlNTRhZDVkMzMiLCJwIjoiaiJ9 ) / Bug ( https://hibernate.atlassian.net/browse/HHH-14105?atlOrigin=eyJpIjoiNmMyNzhhMzIwZWI3NGE0NzgyM2ExZThlNTRhZDVkMzMiLCJwIjoiaiJ9 ) HHH-14105 ( https://hibernate.atlassian.net/browse/HHH-14105?atlOrigin=eyJpIjoiNmMyNzhhMzIwZWI3NGE0NzgyM2ExZThlNTRhZDVkMzMiLCJwIjoiaiJ9 ) JoinColumn for composite key requires alphabetical order ( https://hibernate.atlassian.net/browse/HHH-14105?atlOrigin=eyJpIjoiNmMyNzhhMzIwZWI3NGE0NzgyM2ExZThlNTRhZDVkMzMiLCJwIjoiaiJ9 )

Change By: Mikhail Mayorov ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5cd931e24401890dcab39b3a )

h1. Description

In the following example JoinColumn annotations are order sensitive:

Schema - two tables, one of them has composite key *(name, last_name)*:

{code:sql}
create table product ( id int primary key not null, name varchar(255) not null, last_name uuid not null );
create table profile ( name varchar(255) not null, last_name uuid not null, age int not null);
alter table profile add constraint profile_pkey primary key (name, last_name);
{code}

Entities:

{code:java}
@Entity
@Table
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Profile {

@EmbeddedId
private Id id;
private int age;

@Embeddable
@NoArgsConstructor
@AllArgsConstructor
@Data
public static class Id implements Serializable {
private String name;
private UUID lastName;
}
}

@Entity
@Table
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Product {
@Id
private int id;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "name")
@JoinColumn(name = "last_name")
private Profile profile;
}
{code}

In Product entity it is impossible to specify a relation with the order of *name* and *last_name*. After debugging I've found it must be alphabetically sorted, i.e. *last_name* and *name*.
It would be better to make JoinColumn order independent or depend on natural order at worst, because the order of *name* and *last_name* is everywhere: in schema.sql, in composite key Product.Id.
Why does it require alphabetical order?

h1. Environment

Spring Boot 2.3.1.RELEASE
Hibernate 5.4.17.Final
Postgres 42.2.14

h1. How to reproduce

# 1) Download

[^joincolumn_demo.zip]
# `

2) {{./mvnw test ` }} - this run should pass
#
3) Switch JoinColumn annotations in src/main/java/com/example/demo/Product.java to natural order *name, last_name* the following way:

{code:java}
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "name")
@JoinColumn(name = "last_name")
private Profile profile;
{code}
# `
4){{./mvnw test ` }}

As a result it fails with the error:

{noformat}
ERROR: column "last_name" is of type uuid but expression is of type character varying
Hint: You will need to rewrite or cast the expression
{noformat}

( https://hibernate.atlassian.net/browse/HHH-14105#add-comment?atlOrigin=eyJpIjoiNmMyNzhhMzIwZWI3NGE0NzgyM2ExZThlNTRhZDVkMzMiLCJwIjoiaiJ9 ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-14105#add-comment?atlOrigin=eyJpIjoiNmMyNzhhMzIwZWI3NGE0NzgyM2ExZThlNTRhZDVkMzMiLCJwIjoiaiJ9 )

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.core&referrer=utm_source%3DNotificationLink%26utm_medium%3DEmail ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailNotificationLink&mt=8 ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100133- sha1:cb30471 )
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/hibernate-issues/attachments/20200714/51f397c0/attachment.html 


More information about the hibernate-issues mailing list