Hibernate:在涉及外键的复合主键星座中,insertable = false、updatable = false 在哪里? [英] Hibernate: Where do insertable = false, updatable = false belong in composite primary key constellations involving foreign keys?

查看:33
本文介绍了Hibernate:在涉及外键的复合主键星座中,insertable = false、updatable = false 在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Hibernate 或其他 ORM 中实现复合主键时,在使用识别关系(作为 PK 一部分的 FK)的复合主键星座中,最多可以在三个位置放置 insertable = false、updatable = false:

  1. 进入复合PK类'@Column注解(仅限@Embeddable类)或
  2. 进入实体类的关联@JoinColumn/s注解或
  3. 进入实体类的冗余PK属性的@Column注解(仅限@IdClass类)

第三种是处理@IdClass 和 JPA 1.0 AFAIK 的唯一方法.请参阅 http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_Relationships>.我将只考虑情况 1. 和 2.

问:将insertable = false,updatable = false"放在哪个位置更合适?

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

创建表格邮编(country_code CHAR(2),代码 VARCHAR(10),PRIMARY KEY(国家代码,代码),FOREIGN KEY (country_code) REFERENCES 国家 (iso_code))

与:

org.hibernate.MappingException:实体映射中的重复列:com.kawoolutions.bbstats.model.Zip 列:country_code(应映射为 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")公共类 Zip 实现了可序列化{@EmbeddedId私人 ZipId id;@ManyToOne@JoinColumn(name = "country_code", referencedColumnName = "iso_code")私人国家 country = null;...}

复合PK类:

@Embeddable公共类 ZipId 实现了可序列化{@Column(name = "country_code", 可插入 = false, 可更新 = false)私人字符串国家代码;@Column(name = "code")私人字符串代码;...}

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

本质上,标准 JPA 方式、最佳实践或偏好是什么,将可插入 = 假,可更新 = 假"放在哪里?

让我一步一步来回答.

1.什么时候需要`可插入=假,可更新=假`?

让我们看看下面的映射,

公共类 Zip {@ManyToOne@JoinColumn(name = "country_code", referencedColumnName = "iso_code")私人国家/地区= null@Column(name = "country_code")私人字符串国家代码;}

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

Zip z = new Zip();z.setCountry(getCountry(美国"));z.setCountryCode(IN");saveZip(z);

Hibernate 将在这里做什么??

为了防止这种不一致,Hibernate 要求您指定关系的更新点.这意味着您可以n次引用表中的同一列,但只能使用其中之一进行更新,而其他所有列都是只读的..>

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

在您的 Zip 类中,您指的是再次包含国家/地区代码的嵌入式 ID 类 ZipId.在上面的场景中,现在您可以从两个地方更新 country_code 列.因此Hibernate给出的错误是正确的.

3.如何解决您的问题?

没有.理想情况下,您希望 ZipId 类生成 id,因此您不应将 insertable = false, updatable = false 添加到 ZipId 内的 countryCode.所以修复如下修改 country 映射在你的 Zip 类中,

@ManyToOne@JoinColumn(name = "country_code", referencedColumnName = "iso_code",可插入 = 假,可更新 = 假)私人国家;

希望这有助于您的理解.

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"?

解决方案

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.

这篇关于Hibernate:在涉及外键的复合主键星座中,insertable = false、updatable = false 在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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