The columnDefinition field of joinColumn does not take effect

I would like to report this bug on the dedicated website, but I am unable to log in to the platform used for reporting bugs. Therefore, I can only seek assistance here.

I traced it to the source code, specifically in the createSimpleProperty method of CopyIdentifierComponentSecondPass.class , where it checks (firstColumn.isNameDeferred()) . The columnDefinition of JoinColumn only takes effect after the name attribute of JoinColumn is specified. I believe that it should also check whether columnDefinition is specified, rather than only checking for name .

While attempting to generate the DDL, I noticed that when a property references another entity, it defaults to reading the columnDefinition of the id attribute of the other entity.

@Entity
public class Entity2 {

    @Id
    @Column(columnDefinition = "bigint not null auto_increment comment 'ID'")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;
    String name;
}

@Entity
public class Entity1  {
    @Id
    @Column(columnDefinition = "bigint not null auto_increment comment 'ID'")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;

    @JoinColumn(
            foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT),
//            name = "entity2_id",
            columnDefinition = "bigint not null comment 'entity2 ID'"
    )
    @ManyToOne
    Entity2 entity2;
}

I have provided the code for reproducing the issue.
In these two entities, there is a line that is commented out: // name = "entity2_id" .
If the name attribute is commented out, you will notice that the columnDefinition in this JoinColumn annotation does not take effect.
If you uncomment it, the columnDefinition will work as expected.

My hibernate version is

    <dependency>
      <groupId>org.hibernate.orm</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>6.6.2.Final</version>
      <scope>compile</scope>
    </dependency>

Please try to create a reproducer with our test case template and if you are able to reproduce the issue, create a bug ticket in our issue tracker and attach that reproducer.
If you think you know how to fix this, you can also provide a PR against the Hibernate ORM repository with a test and a fix.

thanks
I post a issue [HHH-19118] The columnDefinition field of joinColumn does not take effect - Hibernate JIRA

1 Like