[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-5751) MappingException: using @Column in Embedded ID classes fail when also used in an @JoinColumn in the entity class

Karsten Wutzke (JIRA) noreply at atlassian.com
Mon Nov 22 13:40:13 EST 2010


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-5751?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=39183#action_39183 ] 

Karsten Wutzke commented on HHH-5751:
-------------------------------------

I've attached a standalone SSCCE using JavaSE, Hibernate 3.6, HSQLDB, and Ant. Just use "ant run" from a shell.

> MappingException: using @Column in Embedded ID classes fail when also used in an @JoinColumn in the entity class
> ----------------------------------------------------------------------------------------------------------------
>
>                 Key: HHH-5751
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5751
>             Project: Hibernate Core
>          Issue Type: Bug
>          Components: annotations, core, metamodel
>    Affects Versions: 3.6.0
>         Environment: Hibernate 3.6, JavaSE, HSQLDB, Ant
>            Reporter: Karsten Wutzke
>            Priority: Critical
>         Attachments: zips-hib-embeddedid-column-broken.zip
>
>
> 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
> The problem here is the @Column on the @Embeddable composite primary key class. country_code is mapped as read-only (insertable = false, updatable = false) in the composite primary key class. The above syntax is JPA-compatible. Clearly, there's no other clean way where to put the @Column annotations but into the @Embeddable class. It's incomprehensible why this doesn't work. @Embeddable classes allow @Basic, @Column, @Enumerated, @Temporal, @Lob, and @Embedded on its columns, so this should work.
> 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...
> The above syntax works perfectly with EclipseLink!

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list