JPA规范是否允许引用非主键列? [英] Does the JPA specification allow references to non-primary key columns?

查看:108
本文介绍了JPA规范是否允许引用非主键列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JPA规范是否允许对非主键列的简单引用?

Does the JPA specification allow simple references to non-primary key columns?

我有一个简单的替代/自然键(UNIQUE,NOT NULL)列iso_code我希望在引用中使用的国家/地区表,但Eclipse的Dali显示验证错误,Hibernate会抛出MappingException。

I have a simple alternative/natural key (UNIQUE, NOT NULL) column iso_code on my Countries table which I'd like to use in a reference, but Eclipse's Dali shows a validation error and Hibernate throws a MappingException.

是否允许这样的常见场景?

Is such a common scenario allowed?

推荐答案

@axtavt:看起来好像您的回答不正确。我刚刚收到了来自Pro JPA 2.0作者的电子邮件,他们也正在从事JPA规范工作。

@axtavt: It appears as if your answer wasn't correct. I've just received an email from the authors of "Pro JPA 2.0", who were also working on the JPA spec themselves.

在您的示例中,Zip类具有与国家的关系:

"In your example the Zip class has a relationship to a Country:

public class Zip implements Serializable
{
    @Id
    @Column(name = "code")
    private String code;

    @Id
    @ManyToOne
    @JoinColumn(name = "country_code", referencedColumnName = "iso_code")
    private Country country = null;
    ...
}

这似乎试图将country_code外键列指向Country表中的iso_code列,而不是PK。 JPA从未允许您创建类似这样的关系,因为没有指定Country
的PK将无法唯一标识关系中的哪个国家/地区实例。当您到达派生标识符部分时,您只是遇到了问题,但

This appears to be trying to point the country_code foreign key column to the iso_code column in the Country table, which is not a PK. JPA has never allowed you to create a relationship like this because without specifying the PK of the Country there would be no way to uniquely identify which Country instance is in the relationship. You are simply hitting the problem when you get to the derived identifier part, but the problem looks to be in the invalid relationship itself."

所以JPA规范根本不允许关系/ FK到非PK列...

So the JPA spec doesn't allow relationships/FKs to non-PK columns at all...

这篇关于JPA规范是否允许引用非主键列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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