Uploaded image for project: 'Hibernate ORM'
  1. HHH-10385

In a @OneToMany association the @JoinColumn foreignKey is not taken into consideration when generating the association database schema

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 5.0.5
    • Fix Version/s: 5.0.7
    • Component/s: None
    • Labels:
      None
    • Bug Testcase Reminder (view):

      Bug reports should generally be accompanied by a test case!

    • Last commented by a user?:
      true
    • Sprint:

      Description

      When declaring a mapping as follows:

      @Entity(name = "Person")
      public class Person  {
      
          @Id
          @GeneratedValue
          private Long id;
      
          public Person() {}
      
          @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
          @JoinTable(name = "person_phone",
              joinColumns = @JoinColumn(name = "person_id", foreignKey = @ForeignKey(name = "PERSON_ID_FK")),
              inverseJoinColumns = @JoinColumn(name = "phone_id", foreignKey = @ForeignKey(name = "PHONE_ID_FK"))
          )
          private List<Phone> phones = new ArrayList<>();
      
          public List<Phone> getPhones() {
              return phones;
          }
      }
      
      @Entity(name = "Phone")
      public class Phone  {
      
          @Id
          @GeneratedValue
          private Long id;
      
          private String number;
      
          public Phone() {}
      
          public Phone(String number) {
              this.number = number;
          }
      
          public Long getId() {
              return id;
          }
      
          public String getNumber() {
              return number;
          }
      }
      

      and generating the schema using hibernate.hbm2ddl.auto set to create-drop, the following schema is being generated:

      CREATE TABLE Person
          (
            id BIGINT NOT NULL ,
            PRIMARY KEY ( id )
          )
      
      CREATE TABLE person_phone
          (
            person_id BIGINT NOT NULL ,
            phone_id BIGINT NOT NULL
          )
      
      CREATE TABLE Phone
          (
            id BIGINT NOT NULL ,
            number VARCHAR(255) ,
            PRIMARY KEY ( id )
          )
      
      ALTER TABLE person_phone 
      ADD CONSTRAINT UK_m5nffi7o8ge7rr71ah3yhur8t 
      UNIQUE (phone_id)
      
      ALTER TABLE person_phone 
      ADD CONSTRAINT FK81wfdg8mla92eu8uuhphw551t 
      FOREIGN KEY (phone_id) REFERENCES Phone
      
      ALTER TABLE person_phone 
      ADD CONSTRAINT FK4t02ig57t70dqj9gr23ov6uwf 
      FOREIGN KEY (person_id) REFERENCES Person
      

      The @ForeignKey name doesn't propagate to the underlying database schema, which still uses a randomly assigned FK name.

      Test will be attached after knowing the issue id.

        Attachments

          Issue links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: