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

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

问题描述

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

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

我的国家/地区表上有一个简单的替代/自然键(唯一,非空)列 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 从来不允许你建立这样的关系 因为没有指定国家的 PK将无法唯一标识关系中的哪个 Country 实例.当您到达派生标识符部分时,您只是遇到了问题,但问题似乎出在无效关系本身."

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 规范根本不允许与非 PK 列的关系/FK...

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

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

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