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

JPA (Hibernate) forign key definition on @JoinTable does not work

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Duplicate
    • Affects versions: 4.3.11
    • Fix versions: None
    • Components: hibernate-core
    • Labels:
      None
    • Bug Testcase Reminder (view):

      Bug reports should generally be accompanied by a test case!

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

      Description

      I am developing an application using JPA 2.1 with Hibernate 4.3 as the persistance provider.

      For the sacke of readability and better maintainance, I want to explicitly name every possible think and not to rely on the hibernate naming strategy.

      I am using the newly introduced @ForignKey annotation to customize the forign key constraint name, and it works fine for @JoinColumn associated with @ManyToOne relationships.

      The problem comes when trying to customise the forign key constraints generated for @ManyToMany relationship using a @JoinTable, the provider do not use my provided name, and revert back to its randomly generated name.

      for example:
      Assuming we have an entity called collection:

      @ManyToOne
      @JoinColumn(name = "store_id", referencedColumnName = "id",
              foreignKey = @ForeignKey(name = "fk_collection_store_id"))
      Store store;
      

      correctly generate the following ddl

      alter table collection add constraint fk_collection_store_id foreign key (store_id) references store
      

      but when i try to use it with @ManyToMany association, it does not work as expected:

      @ManyToMany
      @JoinTable(name="collection_product",
              joinColumns = {@JoinColumn(name="collection_id", referencedColumnName = "id")},
              foreignKey = @ForeignKey(name = "fk_collection_product__collection_id"),
              inverseJoinColumns = {@JoinColumn(name="product_id", referencedColumnName = "id")},
              inverseForeignKey = @ForeignKey(name = "fk_collection_product__product_id"))
      List<Product> products = new ArrayList<>();
      

      the generated ddl doesnot honor my provided names, and revert to the auto generated random names:

      alter table collection_product add constraint FK_skd8u4feadi59mpp8os1q1ar3 foreign key (product_id) references product
      alter table collection_product add constraint FK_lbkv2n46sv06t6qfwabbk0wgw foreign key (collection_id) references collection
      

      So, what is wrong here?

      by the way, I tried to use foregnKey attribute on the @JoinColumn itself (it seems wrong, but i tried it anyway) and it does not help either:

      @ManyToMany
      @JoinTable(name="collection_product",
              joinColumns={@JoinColumn(name="collection_id", referencedColumnName = "id",
                      foreignKey = @ForeignKey(name = "fk_collection_product__collection_id"))},
              inverseJoinColumns={@JoinColumn(name="product_id", referencedColumnName = "id",
                      foreignKey = @ForeignKey(name = "fk_collection_product__product_id"))})
      List<Product> products = new ArrayList<>();
      

      it does not work either:

      alter table collection_product add constraint FK_skd8u4feadi59mpp8os1q1ar3 foreign key (product_id) references product
      alter table collection_product add constraint FK_lbkv2n46sv06t6qfwabbk0wgw foreign key (collection_id) references collection
      

      what is the correct way to make this work?

      Thanks

        Attachments

          Issue links

            Activity

              People

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

                Dates

                • Created:
                  Updated:
                  Resolved: