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} |
|