Here the stripped-down example from the duplicate
HHH-9397
again:
@Entity
public class A {
@Id
@Column(name="AID", length=8)
private String id;
}
@Embeddable
public class B_Key implements Serializable {
public B_Key() {}
private String aId;
@Column(name = "BB", length = 1)
private String b;
}
@Entity
public class B {
@EmbeddedId
private B_Key id;
@MapsId("aId")
@ManyToOne
@JoinColumn(name = "FK_A", nullable=false)
private A a;
}
SchemaExport in Hibernate 4.3.6.FINAL will produce (using H2Dialect, but the same also occurs with other dialects)
create table A (
AID varchar(8) not null,
primary key (AID)
)
create table B (
FK_A varchar(255) not null,
BB varchar(1) not null,
primary key (FK_A, BB)
)
alter table B
add constraint FK_8u5iivujidu0ix4s4f9cw76ns
foreign key (FK_A)
references A
FK_A should be "varchar(8)", not "varchar(255)".
When compiling, Hibernate constructs two org.hibernate.mapping.Column objects for this FK_A column: one in the table B itself, and one in the table's ForeignKey constraint. The one in the ForeignKey is correct after Configuration.secondPassCompileForeignKeys(), while the one in the table never gets updated to the values from the referenced column in A. Adding the referencedColumnName does not help. If the name in the @JoinColumn is omitted, the Column in the table is created already with the correct length set, but is then of course not named "FK_A" but "a_AID".
|