簡體   English   中英

Hibernate是否在嵌入式ID組合主鍵類(錯誤?)中不允許@Column上為只讀?

[英]Does Hibernate disallow read-only on @Column in embedded ID composite primary key classes (bug?)?

這是數據庫設計(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;

    ...
}

休眠堆棧跟蹤:

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在組合主鍵類中映射為只讀(可插入=假,可更新=假)。 這與EclipseLink完美配合! IIRC @Embeddable類在其列上允許@ Basic,@ Column,@ Enumerated,@ Temporal,@ Lob和@Embedded,因此應該可以使用。 請注意,該代碼與JPA 1.0兼容。

將可插入= false,可更新= false放在@JoinColumn上時,異常消失了,但這不是我想要的。 我希望我的協會是可寫的...

這是休眠錯誤嗎? 我正在使用Hibernate 3.6穩定版。

是的,好像是個蟲子。 無論如何,我想您可以這樣做。 不過,我自己還沒有嘗試過。

@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;    

    ...
}

看起來像個錯誤。 作為解決方法,您可以將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; 

    ... 
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM