简体   繁体   English

为什么@JoinTable上的JPA(休眠)外键定义不起作用?

[英]Why does JPA (Hibernate) foreign key definition on @JoinTable not work?

I am developing an application using JPA 2.1 with Hibernate 4.3 as the persistence provider. 我正在开发使用JPA 2.1和Hibernate 4.3作为持久性提供程序的应用程序。

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. 我使用的是新引进的@ForignKey批注自定义forign键约束的名字,它工作正常@JoinColumn关联@ManyToOne关系。

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. 当尝试使用@JoinTable定制为@ManyToMany关系生成的@ManyToMany键约束时,问题就出现了,提供程序不使用我提供的名称,而是恢复为其随机生成的名称。

for example: 例如:

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

correctly generate the following ddl 正确生成以下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关联一起使用时,它无法按预期工作:

@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 does not honor my provided names, and revert to the auto generated random names: 生成的ddl不遵守我提供的名称,而是恢复为自动生成的随机名称:

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 foreignKey attribute on the @JoinColumn itself (it seems wrong, but i tried it anyway) and it does not help either: 顺便说一句,我试图在@JoinColumn本身上使用foreignKey属性(这似乎是错误的,但是无论如何我都尝试过),它也无济于事:

@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 谢谢

Searching the Hibernate issue tracker, I found several bug reports related to this issue. 在Hibernate问题跟踪器中搜索,我发现了几个与此问题相关的错误报告。

It is partially fixed in the 5.x releases. 在5.x版本中已部分修复。 some cases still not picked by the new systems, such as relationships in embedded persistent classes. 新系统仍未选择某些情况,例如嵌入式持久性类中的关系。 I intend to file a bug for this shortly 我打算为此提交一个错误

meanwhile you can upgrade to 5.x version, I use 5.1 and is working fine, and there is little porting issues. 同时,您可以升级到5.x版本,我使用5.1并运行良好,并且几乎没有移植问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM