在 Hibernate 或其他 ORM 中实现复合主键时,在使用标识关系(作为 PK 一部分的 FK)的复合主键星座中,最多可以在三个位置放置 insertable = false、updatable = false:
- 进入复合 PK 类的 @Column 注释(仅限 @Embeddable 类)或
- 进入实体类的关联@JoinColumn/s注解或者
- 进入实体类'多余的PK 属性的 @Column 注释(仅限 @IdClass 类)
第三种是使用 @IdClass 和 JPA 1.0 AFAIK 的唯一方法。看http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_through_OneToOne_Relationships 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)
)
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)
...
正如您所看到的,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 = false 放入实体类关联的 @JoinColumn 时,所有异常都会消失,一切正常。但是,我不明白为什么上面的代码不应该工作。 Hibernate 可能有这个问题。所描述的是 Hibernate bug,因为它似乎没有评估 @Column“insertable = false,updatable = false”?
本质上,标准 JPA 方式、最佳实践或偏好将“insertable = false,updatable = false”放在哪里?