This is a huge drawback... the element
Code:
<key-many-to-one/>
inside the
Code:
<composite-id/>
element doesn't have a "type" definition attribute like
Code:
<key-property/>
has.
The problem is, when you don't specify the type definition (like integer, or string HB native types) of a specific key, Hibernate tries to map the database/PO definition to its internal definition, example, if MySQL uses a type of "INT", then HB tries to map this type to "int" and not to "integer", the correct mapping type, resulting to a set of terrible erros like the ones described bellow:
Code:
Caused by: org.hibernate.MappingException: An association from the table XXX_ENTITY_XXX refers to an unmapped class: int
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1343)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1261)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1377)
at com.unoware.services.ubw.HibernateSessionFactoryWrapper.<init>(HibernateSessionFactoryWrapper.java:57)
at com.unoware.services.ubw.HibernateSessionFactoryWrapper.<clinit>(HibernateSessionFactoryWrapper.java:51)
at java.lang.J9VMInternals.initializeImpl(Native Method)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:200)
... 4 more
This problem only occurs when using
Code:
<key-many-to-one/>
because the "type" attribute isn't defined on the HB DTD. A solution for that problem is to only use the
Code:
<key-property/>
element inside a composed ID definition. The problem with this approach is that all the keys from the parent entity must be also necessary defined on both association directions, "one-to-many" and "many-to-one" (if both sides have an association). This situation (parent keys == child foreign keys) are almost unlike to occur on real world scenarios.
Code:
Example 1 (Okay):
Parent Child
========= ========
id1 (PK) => id1(FK)
id2 (PK) => id2(FK)
Example 2 (Problem):
Parent Child
========== ========
id1 (PK) => id1(FK)
id2 (PK)
I solved my problem replacing all the
Code:
<key-many-to-one/>
to
Code:
<key-property/>
elements (that have the native "type" attribute). This is a huge drawback, and I really hope that in future releases this problem is corrected.
Hibernate Version: 3.x