Hibernate是否在嵌入式ID复合主键类中禁止对@Column进行只读(错误?)?

时间:2010-11-22 13:31:03

标签: java hibernate jpa hibernate-mapping

这是数据库设计(DDL):

CREATE TABLE Countries
(
  iso_code CHAR(2) NOT NULL,
  name VARCHAR(50) NOT NULL,
  PRIMARY KEY (iso_code)
);

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

这是Zip类+复合主键类:

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

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

    ...
}

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

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

    ...
}

Hibernate堆栈跟踪:

Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: zips] Unable to build EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:911)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
    at tld.zips.Main.main(Main.java:27)
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: tld.zips.model.Zip column: country_code (should be mapped with insert="false" update="false")
    at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:675)
    at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:697)
    at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:719)
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:473)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:235)
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1332)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1835)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:902)
    ... 4 more

这是什么? country_code在复合主键类中映射为只读(insertable = false,updatable = false)。这与EclipseLink完美配合! IIRC @Embeddable类允许@Basic,@ Column,@ Enumerated,@ Temporal,@ Lob和@Embedded在其列上,所以这应该有效。请注意,代码与JPA 1.0兼容。

在@JoinColumn上插入insertable = false,updatable = false时异常消失,但这不是我想要的。我更喜欢我的协会是可写的......

这是一个Hibernate错误吗?我正在使用Hibernate 3.6 stable。

2 个答案:

答案 0 :(得分:0)

是的,好像是一个bug。无论如何,我想你可以这样做。但是,我自己没试过。

@Entity
@Table(name = "Zips")
public class Zip implements Serializable
{
    @EmbeddedId
    @AttributeOverrides({
        @AttributeOverride(name="countryCode", column=@Column(name="country_code", insertable = false, updatable = false))
        @AttributeOverride(name="code", column=@Column("code"))
    })
    private ZipId embeddedId;

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

    ...
}

@Embeddable
public class ZipId implements Serializable
{
    private String countryCode;
    private String code;    

    ...
}

答案 1 :(得分:0)

看起来像个错误。要解决此问题,您可以将country放入ZipId而不是countryCode

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

@Embeddable 
public class ZipId implements Serializable 
{ 
    @ManyToOne 
    @JoinColumn(name = "country_code", referencedColumnName = "iso_code") 
    private Country country = null; 

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

    ... 
}