JPA是否支持数据库中没有外键约束的实体之间的@OneToMany映射? [英] Does JPA support a @OneToMany mapping between entities without a foreign key constraint in the database?

查看:1212
本文介绍了JPA是否支持数据库中没有外键约束的实体之间的@OneToMany映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是针对传统数据库的。即使子表列包含父表的PK,此关系中的两个数据库表也没有外键约束。



JPA是否支持@OneToMany如果子表在相应的列上没有外键约束,映射(双向还是单向)?解析方案

简短回答:

长一个:如果数据库已经存在,JPA是否真的关心外键?如果我们将创建数据库模式的任务留给JPA提供者,它将创建外键,这很好。但是,如果JPA在现有数据库上工作,它只关心是否存在用于生成正确联接的外键和主键列。



事实上,当我和Hibernate一起工作时,我们有一个使用 short 的遗留数据库>键入外键列以指向另一个表上的 int 主键。不能有任何外键,但只要我们没有使用 validate hbm2ddl 选项,Hibernate as JPA提供者工作得很好,忽略了类型不匹配,更不用说缺少外键了。


This is for a legacy database. The two database tables in this relationship don't have a foreign key constraint even 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.

这篇关于JPA是否支持数据库中没有外键约束的实体之间的@OneToMany映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆