Hibernate:insertable = false、updatable = false 属于涉及外键的复合主键星座中的什么位置?

发布于 2024-09-18 02:03:25 字数 2190 浏览 9 评论 0原文

的 FK)的复合主键星座中,最多可在三个位置放置 insertable = false、updatable = false:

  1. 在 Hibernate 或其他 ORM 中实现复合主键时,在使用标识关系(属于PK class' @Column 注解(仅@Embeddable类)或
  2. 进入实体类'关联@JoinColumn/s注解或
  3. 进入实体类'冗余 PK属性的@Column注解(仅@IdClass类

) AFAIK 是处理 @IdClass 和 JPA 1.0 的唯一方法。请参阅http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_through_OneToOne_Relationships。我只会考虑情况 1. 和 2。

问: 一般来说,哪种方式是放置“insertable = false,updatable = false”的首选位置?

我在 Hibernate 中遇到过关于这个问题的问题。例如,Hibernate 3.5.x 会抱怨 Zips 表

CREATE TABLE Zips
(
  country_code CHAR(2),
  code VARCHAR(10),
  PRIMARY KEY (country_code, code),
  FOREIGN KEY (country_code) REFERENCES Countries (iso_code)
)

org.hibernate.MappingException: Repeated column in mapping for entity: com.kawoolutions.bbstats.model.Zip column: country_code (should be mapped with insert="false" update="false")
org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:676)
org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:698)
...

如您所见,country_code 列同时是 PK 和 FK。以下是它的类:

实体类:

@Entity
@Table(name = "Zips")
public class Zip implements Serializable
{
    @EmbeddedId
    private ZipId id;

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

Composite PK 类:

@Embeddable
public class ZipId implements Serializable
{
    @Column(name = "country_code", insertable = false, updatable = false)
    private String countryCode;

    @Column(name = "code")
    private String code;
...
}

将 insertable = false、updatable = false 放入实体类关联的 @JoinColumn 时,所有异常都会消失,一切正常。但是,我不明白为什么上面的代码不应该工作。 Hibernate 可能有这个问题。所描述的是 Hibernate bug,因为它似乎没有评估 @Column“insertable = false,updatable = false”?

本质上,标准 JPA 方式、最佳实践或偏好在哪里放置“insertable = false,updatable = false”是什么?

When implementing composite primary keys in Hibernate or other ORMs there are up to three places where to put the insertable = false, updatable = false in composite primary key constellations that use identifying relationships (FKs that are part of the PK):

  1. Into the composite PK class' @Column annotation (@Embeddable classes only) or
  2. Into the entity class' association @JoinColumn/s annotation or
  3. Into the entity class' redundant PK property's @Column annotation (@IdClass classes only)

The third is the only way to do with @IdClass and JPA 1.0 AFAIK. See http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_through_OneToOne_Relationships. I will consider only cases 1. and 2.

Q:
Which way is the preferred place to put the "insertable = false, updatable = false" to generally?

I have experienced problems with Hibernate concerning this question. For example, Hibernate 3.5.x will complain about the Zips table

CREATE TABLE Zips
(
  country_code CHAR(2),
  code VARCHAR(10),
  PRIMARY KEY (country_code, code),
  FOREIGN KEY (country_code) REFERENCES Countries (iso_code)
)

with:

org.hibernate.MappingException: Repeated column in mapping for entity: com.kawoolutions.bbstats.model.Zip column: country_code (should be mapped with insert="false" update="false")
org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:676)
org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:698)
...

As you can see the country_code column is both PK and FK. Here are its classes:

Entity class:

@Entity
@Table(name = "Zips")
public class Zip implements Serializable
{
    @EmbeddedId
    private ZipId id;

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

Composite PK class:

@Embeddable
public class ZipId implements Serializable
{
    @Column(name = "country_code", insertable = false, updatable = false)
    private String countryCode;

    @Column(name = "code")
    private String code;
...
}

When putting the insertable = false, updatable = false into the entity class association's @JoinColumn all exceptions disappear and everything work fine. However, I don't see why the above code should not be working. It might be Hibernate having problems with this. Is the described a Hibernate bug, as it doesn't seem to evaluate @Column "insertable = false, updatable = false"?

In essence, what's the standard JPA way, the best practice, or preference where to put "insertable = false, updatable = false"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罪歌 2024-09-25 02:03:25

让我一步一步来回答吧。

1.什么时候需要`insertable=false,updatable=false`?

让我们看一下下面的映射,

public class Zip {

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

    @Column(name = "country_code")
    private String countryCode;

}

这里我们使用两个不同的属性引用表中的同一列。在下面的代码中,

Zip z = new Zip();

z.setCountry(getCountry("US"));
z.setCountryCode("IN");

saveZip(z);

Hibernate 会在这里做什么?

为了防止这种不一致,Hibernate 要求您指定关系的更新点。这意味着您可以引用表中的同一列n次,但只有其中一列可用于更新,而其他所有列均为只读

2. 为什么 Hibernate 抱怨你的映射?

在您的 Zip 类中,您引用了同样包含国家/地区代码的嵌入式 id 类 ZipId。正如上面的场景,现在您可以从两个位置更新 country_code 列。因此 Hibernate 给出的错误是正确的。

3.您的情况如何解决?

不可以。理想情况下,您希望您的 ZipId 类生成 id,因此您不应将 insertable = false, updatable = false 添加到 ZipId.因此,修复方法如下,修改 Zip 类中的 country 映射,如下所示,

@ManyToOne
@JoinColumn(name = "country_code", referencedColumnName = "iso_code",
insertable =  false, updatable = false)
private Country country;

希望这有助于您的理解。

Let me answer step by step.

1. When do you need ` insertable = false, updatable = false`?

Let's look at the below mapping,

public class Zip {

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

    @Column(name = "country_code")
    private String countryCode;

}

Here we are referring to the same column in the table using two different properties. In the below code,

Zip z = new Zip();

z.setCountry(getCountry("US"));
z.setCountryCode("IN");

saveZip(z);

What will Hibernate do here??

To prevent these kind of inconsistency, Hibernate is asking you to specify the update point of relationships. Which means you can refer to the same column in the table n number of times but only one of them can be used to update and all others will be read only.

2. Why is Hibernate complaining about your mapping?

In your Zip class you are referring to the Embedded id class ZipId that again contains the country code. As in the above scenario now you have a possibility of updating the country_code column from two places. Hence the error given by Hibernate is proper.

3. How to fix it in your case?

No. Ideally you want your ZipId class to generate the id, so you should not add insertable = false, updatable = false to the countryCode inside the ZipId. So the fix is as below modify the country mapping in your Zip class as below,

@ManyToOne
@JoinColumn(name = "country_code", referencedColumnName = "iso_code",
insertable =  false, updatable = false)
private Country country;

Hope this helps your understanding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文