问题描述
我刚刚发现JPA不支持与非主键列的正式关系/外键.看到这里:
I just found out that officially relationships/foreign keys to non-primary key columns aren't supported by JPA. See here:
尽管如此,为什么这样的关系仍在Hibernate和EclipseLink中进行映射(仅使用JPA 1.0语法,如此处示例所示)?
Why do such relationships map in Hibernate and EclipseLink nontheless (only in JPA 1.0 syntax as in the example posted there)??
这些JPA提供程序似乎只是天真地映射了列,我认为这是一件好事,但我想明确知道.这是巧合吗?是有意的吗?
It appears that these JPA providers just map the columns naively, which I think is a good thing, but I'd like to know explicitly. Is it coincidence? Is it intended?
推荐答案
EclipseLink/TopLink始终允许外键有意地指向任何表字段,因为对象的主键不一定需要是表-任何唯一标识符都可以.
EclipseLink/TopLink has always allowed foreign keys to point to any table field intentionally, as an object's primary keys didn't neccessarily need to be the pks used on the table - any unique identifier would do.
这只是一个猜测,但是使用主键可能是JPA规范确保强制执行唯一标识符的一种方法,并且不使用pk还会带来其他性能影响,因为缓存/对象标识通常仅使用主键-因此可能会导致额外的数据库命中.
Just a guess, but using the primary key might have been one way for the JPA spec to ensure that a unique identifier is enforced, and not using the pk also has other performance implications as caching/object identity is usually only done using the primary key - so it might result in extra database hits.
这篇关于JPA提供者:为什么到非PK列的关系/FK在Hibernate和EclipseLink中起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!