JoinColumn on non key field fails to populate collection

Description

To duplicate the issue, create three tables in a database with no declared primary or foreign keys. Map entities to two of the tables. One of these entities has a primitive @Id; the other has an @EmbeddedId. Both entities contain an additional field used to join with the third table and populate a collection.

This works with the entity that has a primitive @Id but for the entity with @EmbeddedId, trying to do anything with the collection after calling the getter results in:

javax.ejb.EJBException: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of jboss.example.DoesNotWorkPk.id1
at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:77)
....
Caused by: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of jboss.example.DoesNotWorkPk.id1
at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:58)
....
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field jboss.example.DoesNotWorkPk.id1 to jboss.example.DoesNotWork
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146)

I've attached a sample project. The relevant files are Works.java, DoesNotWork.java, and DoesNotWorkPk.java
There's also data.sql along with the java files to populate a mysql db, but I think any DB will do.

Environment

EAP 5 (hibernate 3.3.2), hibernate 3.6.x, 4.0.x , probably eap4 as well

Activity

Show:
Steve Ebersole
July 23, 2012, 6:05 PM
Edited
Strong Liu
July 23, 2012, 6:09 PM

I have some analysis on this issue:

when initialising an EntityKey (with this constructor public EntityKey(Serializable id, EntityPersister persister, EntityMode entityMode)), somehow, the serializable id instance is actually the Entity (with embedded id) instance itself, and identifierType get by `this.identifierType = persister.getIdentifierType();` is correct – the embedded id type.

the method org.hibernate.engine.EntityKey#generateHashCode is called during initialising time, and it then calls `identifierType.getHashCode( identifier, entityMode, factory )`

since identifierType is actually ComponentType, so, in the method getHashCode, this class will using getters to get each filed values from identifier, then there is a type mismatch happens, the identifier type here is supposed to be the embedded pk instance, which it is actually the entity class instance.

so, the https://gist.github.com/3164408 exception throws

Strong Liu
July 23, 2012, 6:12 PM

org.hibernate.type.CollectionType#getKeyOfOwner returns entity instance as collection key instead of composite pk instance, which is expected

Strong Liu
July 25, 2012, 3:41 AM

you commited this to the master branch, which i think it should be ported to 4.1 branch as well

Brett Meyer
March 7, 2014, 10:08 PM

Bulk closing tickets resolved in released versions

Assignee

Steve Ebersole

Reporter

Chris Sams

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Priority

Major
Configure