Java Hibernate是否禁止嵌入式ID复合主键类中@Column的只读(bug?)?
以下是DB设计DDL:Java Hibernate是否禁止嵌入式ID复合主键类中@Column的只读(bug?)?,java,hibernate,jpa,hibernate-mapping,Java,Hibernate,Jpa,Hibernate Mapping,以下是DB设计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), FOREIG
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_代码在复合主键类中映射为只读insertable=false、Updateable=false。这与EclipseLink完美配合!IIRC@embeddeble类允许在其列上嵌入@Basic、@Column、@Enumerated、@Temporal、@Lob和@Embedded,因此这应该可以工作。注意,该代码与JPA1.0兼容
当将insertable=false、updateable=false放在@JoinColumn上时,异常消失,但这不是我想要的。我希望我的关联是可写的
这是冬眠虫吗?我正在使用Hibernate 3.6稳定版。是的,看起来像个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;
...
}
看起来像只虫子。作为一种解决方法,您可以将country放入ZIPD而不是countryCode:
这不是问题!正如我所写的,我知道这一点,但我不希望这样:当将insertable=false、updateable=false放在@JoinColumn上时,异常消失,但这不是我想要的。我希望我的关联是可写的。。。我正在寻找一个答案,如果这是一个休眠错误。我坚信我发布的@Column语法应该符合JPA的要求。@Kawu:事实上,我没有领会你的意思。我已经相应地更新了我的帖子。看看这是否有帮助。虽然这可以在未经测试的情况下工作,但我相信使用AttributeOverrides进行映射并不是JPA设计的目的。我读过一本关于Java持久化与Hibernate的坏书,书中使用了大量@AttributeOverrides,原因令人费解,第8章。我试图在这里映射一些东西,覆盖属性属性似乎不合适。同样,这对EclipseLink非常有效。它看起来确实像一个bug。它确实在Hibernate中受支持。但据我所知,JPA没有。但这绝对是一个有效的解决办法。我只追求JPA兼容性。我不关心特定于Hibernate的语法。。。这样的代码不允许切换JPA提供的内容。不是个好主意。@Kawu:已删除。我也会在一段时间内移除这个。
@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;
...
}