开发者

Does JPA support a @OneToMany mapping between entities without a foreign key constraint in the database?

开发者 https://www.devze.com 2023-02-13 06:30 出处:网络
This is for a legacy database.The two database tables in this relationship don\'t have a foreign key constraint ev开发者_运维知识库en though the child table column holds the PK of the parent table.

This is for a legacy database. The two database tables in this relationship don't have a foreign key constraint ev开发者_运维知识库en though the child table column holds the PK of the parent table.

Does JPA support a @OneToMany mapping (either bidirectional or unidirectional) if the child table does not have a foreign key constraint on the appropriate column?


Short answer: yes.

Long one: does JPA actually care about foreign keys if the database already exists? If we leave the task of creating the database schema to JPA provider, it will create foreign keys, which is good. But if the JPA works on existing database, it only cares about the existence of foreign key and primary key columns to produce proper joins. Foreign keys aren't necessary.

In fact while I worked with Hibernate we've had a legacy database using short type for foreign key column to point to int primary key on the other table. There couldn't be any foreign key, but as long as we didn't use validate hbm2ddl option, Hibernate as JPA provider was working just fine, ignoring type mismatch, not to mention the lack of foreign keys.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号