Hibernate:Where insertable = false,updatable = false是否属于涉及外键的复合主键星座? [英] Hibernate: Where do insertable = false, updatable = false belong in composite primary key constellations involving foreign keys?

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

问题描述

在Hibernate或其他ORM中实现复合主键时,在使用标识关系的复合主键星座(FK是PK的一部分)中,最多有三处将insertable = false,updatable = false: / p>


  1. 进入组合PK类'@Column注解(仅限@Embeddable类)或
  2. 实体类'关联@ContactColumn / s注释或

  3. 到实体类'冗余 PK属性的@Column注释(仅限于@IdClass类)

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



Q:
哪种方式是insertable = false,updatable = false的首选位置一般吗?


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

  CREATE TABLE拉丁文

country_code CHAR(2),
code VARCHAR(10),
PRIMARY KEY(country_code,code),
FOREIGN KEY(country_code)参考国家(iso_code)

$ b

 org.hibernate.MappingException:实体映射中的重复列:com.kawoolutions.bbstats.model.Zip column:country_code(应该用insert =falseupdate =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;
...
}

复合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 =假实体类关联的@JoinColumn中的所有异常消失,一切正常。但是,我不明白为什么上面的代码不应该工作。这可能是Hibernate遇到问题。描述了一个Hibernate错误,因为它似乎没有评估@Columninsertable = false,updatable = false?

从本质上讲,标准的JPA方式是什么,最佳做法或优先选择何处放置insertable = false,updatable = false?

解决方案

让我回答一下步骤。

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



让我们看看下面的映射,

 public class Zip {

@ManyToOne
@JoinColumn(name =country_code,referencedColumnName =iso_code)
private国家国家= null

@Column(name =country_code)
private String countryCode;




$ b $ p
$ b

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

  Zip z = new Zip(); 

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

saveZip(z);

hibernate会在这里做什么??

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

2。为什么hibernate会抱怨你的映射?



在你的 Zip 类中,你指的是Embedded id class ZipId ,它又包含国家代码。正如在上面的场景中,现在您可以从两个地方更新 counry_code 列。因此,由hibernate给出的错误是适当的。

3。如何解决你的情况?



没有。理想情况下,您希望您的 ZipId 类生成该标识,因此您不应将 insertable = false,updatable = false 添加到countryCode位于 ZipId 内。因此,修正如下所示修改 Zip 类中的国家映射,如下所示:

  @ManyToOne 
@JoinColumn(name =country_code,referencedColumnName =iso_code,
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"?

解决方案

Let me answer step by step.

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

Lets 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 relation ships. 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 counry_code column from two places. Hence 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:Where insertable = false,updatable = false是否属于涉及外键的复合主键星座?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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