Hibernate fails with two kinds of exceptions on a compsite-key join table that references two composite keys (one column shared by both foreign keys)

Description

Hibernate fails with two kinds of exceptions on a compsite-key join table that references two composite keys (one column shared by both foreign keys): see ZipArea.java and PostAddress.java inside the attached file

1. When having both entity classes use the annotations as they should be (ZipArea having @Entity and @Table, PostAddress association having @ManyToOne and @JoinColumns) then I get:

18.08.2010 18:22:51 org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet Geo Info Test Servlet as unavailable
18.08.2010 18:22:51 org.apache.catalina.core.StandardContext loadOnStartup
SCHWERWIEGEND: Servlet /geoinfotest threw load() exception
org.hibernate.MappingException: Unable to find column with logical name: state_code in ZipAreas
at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:573)
at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:125)
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:110)
at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1618)
at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1541)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1457)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1413)
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1453)
at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:193)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1081)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:275)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:359)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
at geoinfotest.GeoInfoTestServlet.<init>(GeoInfoTestServlet.java:16)
...

This is the most constant and frequent error. Note though, when I removed all the INDEXes from the database it sometimes happens that Hibernate can't find the column in Cities:

18.08.2010 19:37:43 org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet Geo Info Test Servlet as unavailable
18.08.2010 19:37:43 org.apache.catalina.core.StandardContext loadOnStartup
SCHWERWIEGEND: Servlet /geoinfotest threw load() exception
org.hibernate.MappingException: Unable to find column with logical name: state_code in Cities
at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:573)
at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:125)
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:110)
at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1618)
at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1541)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1457)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1413)
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1453)
at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:193)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1081)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:275)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:359)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
at geoinfotest.GeoInfoTestServlet.<init>(GeoInfoTestServlet.java:16)
...

2. When outcommenting PostAddress's @ManyToOne and @JoinColumns on zipArea and leaving @Entity and @Table on ZipArea class, I get the following exception:

18.08.2010 18:05:02 org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet Geo Info Test Servlet as unavailable
18.08.2010 18:05:02 org.apache.catalina.core.StandardContext loadOnStartup
SCHWERWIEGEND: Servlet /geoinfotest threw load() exception
org.hibernate.AnnotationException: referencedColumnNames(country_code, state_code, name) of geoinfotest.ZipArea.city referencing geoinfotest.City not mapped to a single property
at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:204)
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:110)
at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1618)
at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1541)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1457)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1413)
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1453)
at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:193)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1081)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:275)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:359)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
at geoinfotest.GeoInfoTestServlet.<init>(GeoInfoTestServlet.java:16)
...

I have no idea what's going on. The special situation of the ZipAreas join table is that it references country_code of both tables. As a result, I have only one country_code column, which is used twice by ZipArea's foreign keys. Maybe that's confusing Hibernate. I can only guess. The strange thing is that Hibernate fails with City not being mapped to a single property or not being able to map state_code. My assumption is that Hibernate gobbles on that special design in conjunction with indexes. Just guessing though.

Here's my original forum post: https://forum.hibernate.org/viewtopic.php?f=1&t=1006411
The DB design can be viewed here: http://www.kawoolutions.com/media/geoinfo.pdf

I have attached a ZIP of the test project including an Ant build script (including deploy-local, undeploy-local, and redeploy-local targets using the Tomcat deployment tasks). I outcommented the Tomcat deploy task setup and targets.

Use "ant war" to build the distribution WAR file. I also put a MySQL DDL script into the db dir, including the above PDF, and a MySQL Workbench model. The example doesn't need data to fail. The DB name is "geoinfo". Just set your MySQL DB username and password in xml/persistence.xml (I think it's not even needed).

Environment

Windows 7 64bit, localhost, Tomcat 6, embedded JBoss, Hibernate 3.5.4 and 3.6.0 Beta2, MySQL 5.1.46

Activity

Show:
Ka Wu
August 19, 2010, 6:06 AM

test program

Ka Wu
June 2, 2011, 12:26 AM

Superseded by this bug (better description + test case): http://opensource.atlassian.com/projects/hibernate/browse/HHH-6221

Brett Meyer
March 8, 2014, 4:30 AM

Bulk closing rejected tickets in "resolved" state.

Duplicate

Assignee

Gail Badner

Reporter

Ka Wu

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Affects versions

Priority

Blocker