Java Hibernate:在涉及外键的复合主键星座中,insertable = false、updatable = false 在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3669883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate: Where do insertable = false, updatable = false belong in composite primary key constellations involving foreign keys?
提问by Kawu
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):
在 Hibernate 或其他 ORM 中实现复合主键时,在使用标识关系(作为 PK 一部分的 FK)的复合主键星座中,最多可以在三个位置放置 insertable = false、updatable = false:
- Into the composite PK class' @Column annotation (@Embeddable classes only) or
- Into the entity class' association @JoinColumn/s annotation or
- Into the entity class' redundantPK property's @Column annotation (@IdClass classes only)
- 进入复合PK类'@Column注解(仅限@Embeddable类)或
- 进入实体类的关联@JoinColumn/s 注解或
- 进入实体类的冗余PK属性的@Column注解(仅限@IdClass类)
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.
第三种是处理 @IdClass 和 JPA 1.0 AFAIK 的唯一方法。请参阅http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_through_OneToOne_Relationships。我将只考虑情况 1 和 2。
Q: Which way is the preferred place to put the "insertable = false, updatable = false" to generally?
问:一般把“insertable = false,updatable = false”放在哪个位置比较好?
I have experienced problems with Hibernate concerning this question. For example, Hibernate 3.5.x will complain about the Zips table
我在 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)
)
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:
如您所见, country_code 列既是 PK 又是 FK。这是它的类:
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:
复合PK类:
@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"?
将 insertable = false, updatable = false 放入实体类关联的 @JoinColumn 时,所有异常都会消失,一切正常。但是,我不明白为什么上面的代码不应该工作。可能是 Hibernate 遇到了这个问题。所描述的是否是 Hibernate 错误,因为它似乎没有评估 @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"?
从本质上讲,什么是标准 JPA 方式、最佳实践或偏好设置“可插入 = 假,可更新 = 假”?
采纳答案by ManuPK
Let me answer step by step.
让我一步一步来回答。
1. When do you need ` insertable = false, updatable = false`?
1.什么时候需要`可插入=假,可更新=假`?
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??
hibernate 会在这里做什么?
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.
为了防止这种不一致,hibernate 要求您指定关系船的更新点。这意味着您可以n
多次引用表中的同一列,但只能使用其中之一进行更新,而所有其他列都将是只读的。
2. Why is hibernate complaining about your mapping?
2. 为什么hibernate 抱怨你的映射?
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.
在您的Zip
课程中,您指的ZipId
是再次包含国家/地区代码的 Embedded id 类。在上面的场景中,现在您可以counry_code
从两个地方更新列。因此hibernate给出的错误是正确的。
3. How to fix it in your case?
3. 在你的情况下如何修复它?
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,
不。理想情况下,您希望您的ZipId
类生成 id,因此您不应添加insertable = false, updatable = false
到ZipId
. 所以修复如下修改country
你的Zip
类中的映射如下,
@ManyToOne
@JoinColumn(name = "country_code", referencedColumnName = "iso_code",
insertable = false, updatable = false)
private Country country;
Hope this helps your understanding.
希望这有助于您的理解。
回答by Darshan
You can also solve this problem by using @PrimaryKeyJoinColumn
annotation . The PrimaryKeyJoinColumn annotation specifies a primary key column that is used as a foreign key to join to another table.
您也可以通过使用@PrimaryKeyJoinColumn
annotation来解决这个问题。PrimaryKeyJoinColumn 注释指定一个主键列,该列用作连接到另一个表的外键。
The PrimaryKeyJoinColumn annotation is used to join the primary table of an entity subclass in the JOINED mapping strategy to the primary table of its superclass; it is used within a SecondaryTable annotation to join a secondary table to a primary table; and it may be used in a OneToOne mapping in which the primary key of the referencing entity is used as a foreign key to the referenced entity. If no PrimaryKeyJoinColumn annotation is specified for a subclass in the JOINED mapping strategy, the foreign key columns are assumed to have the same names as the primary key columns of the primary table of the superclass.
PrimaryKeyJoinColumn注解用于将JOINED映射策略中实体子类的主表join到其超类的主表;它在 SecondaryTable 注释中用于将辅助表连接到主表;它可以用在 OneToOne 映射中,其中引用实体的主键用作被引用实体的外键。如果在 JOINED 映射策略中没有为子类指定 PrimaryKeyJoinColumn 注解,则假定外键列与超类主表的主键列具有相同的名称。