Looks like a bug. As a workaround you can place country into ZipId instead of countryCode.
Up vote 1 down vote favorite share g+ share fb share tw.
Here's the DB design (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) ); Here's the Zip class + composite primary key class: @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 stack trace: 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 What's this?
Country_code is mapped as read-only (insertable = false, updatable = false) in the composite primary key class. This works perfectly with EclipseLink! IIRC @Embeddable classes allow @Basic, @Column, @Enumerated, @Temporal, @Lob, and @Embedded on its columns, so this should work.
Note the code is JPA 1.0 compatible. The exception vanishes when putting the insertable = false, updatable = false on the @JoinColumn, but this is not what I want. I prefer my associations to be writable... Is this a Hibernate bug?
I'm using Hibernate 3.6 stable. Java hibernate jpa hibernate-mapping link|improve this question asked Nov 22 '10 at 13:31Kawu1,071627 83% accept rate.
Looks like a bug. As a workaround you can place country into ZipId instead of 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; ... }.
Its supported in Hibernate, indeed. But not in JPA, As far as I know. But definitely a valid workaround.
– Adeel Ansari Nov 22 '10 at 17:00 Yuck. I only go for JPA compatibility. I don't care about Hibernate-specific syntaxes... such code disallows switching JPA provides.
Not a good idea. – Kawu Nov 22 '10 at 17:27 @Kawu: Deleted. Will remove this one too, in a while.
– Adeel Ansari Nov 23 '10 at 3:43.
Yeah, seems like a bug. Anyway, you can do it like this, I suppose. Haven't tried it myself, though.
@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; ... }.
This was not the question! As I wrote, I know this and I don't want it that way: "The exception vanishes when putting the insertable = false, updatable = false on the @JoinColumn, but this is not what I want. I prefer my associations to be writable..." I'm looking for an answer if this is a Hibernate bug.
I firmly believe the @Column syntax I posted should work according to the JPA. – Kawu Nov 22 '10 at 15:26 @Kawu: Missed your point, actually. I have updated my post, accordingly.
See if this helps. – Adeel Ansari Nov 22 '10 at 16:58 While this could work (untested), I believe using AttributeOverrides for mapping is not what JPA was designed for. I read the (bad) book "Java Persistence with Hibernate" which uses a lot of @AttributeOverrides for incomprehensible reasons (ch.
8). I'm trying to map something here, "overriding attribute properties" just appears to be inappropriate IMHO. Again, this works perfectly with EclipseLink.
It looks like a bug indeed. – Kawu Nov 22 '10 at 17:24.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.