繁体   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