Description In the following example JoinColumn annotations are order sensitive: Schema - two tables, one of them has composite key (name, last_name):
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);
Entities:
@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;
}
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? Environment Spring Boot 2.3.1.RELEASE Hibernate 5.4.17.Final Postgres 42.2.14 How to reproduce
- Download joincolumn_demo.zip
![](cid:jira-generated-image-static-link_attachment_7-ff9f2f70-69e6-4cc5-8f48-2fb584e6d984)
- `./mvnw test` - this run should pass
- Switch JoinColumn annotations in src/main/java/com/example/demo/Product.java the following way:
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "name")
@JoinColumn(name = "last_name")
private Profile profile;
- `./mvnw test`
As a result it fails with the error:
|