We're updating the issue view to help you get more done. 

EntityManagerFactoryImpl.getIdentifier uses deprecated version of getIdentifier

Description

When trying to get the identifier of an entity from the PersistenceUnitUtil, I get a NullPointerException. This is because the implementation of getIdentifier uses the deprecated version of ClassMetadata.getIdentifier() which takes no session, and subsequently the implementation of it explicitly sets the session to null. When you get to the IncrediblySillyJpaMapsIdMappedIdentifierValueMarshaller.getIdentifier()-method, persistEventListeners( session ) is called, which then throws the NullPointerException.

This problem is documented as such:

1 2 3 4 // we need a session to handle this use case if ( session == null ) { throw new AssertionError("Deprecated version of getIdentifier (no session) was used but session was required"); }

However, this happens after the persistEventListeners( session )-call. Shouldn't the new version of getIdentifier() be used when there already is support for this in the code?

The implementation of PersistenceUnitUtil.getIdentifier() in EntityManagerFactoryImpl::HibernatePersistenceUnitUtil:

1 2 3 4 5 6 7 8 9 10 @Override public Object getIdentifier(Object entity) { final Class entityClass = Hibernate.getClass( entity ); final ClassMetadata classMetadata = emf.getSessionFactory().getClassMetadata( entityClass ); if ( classMetadata == null ) { throw new IllegalArgumentException( entityClass + " is not an entity" ); } //TODO does that work for @IdClass? return classMetadata.getIdentifier( entity ); }

Side note: I can see there is a "TODO:"-comment here, and the answer is no. This has to do with the problem in HHH-10623. It should work for simple IdClass'es though (with no ManyToOne-relationships in the IdClass).

Environment

Hibernate 5.0.11, Spring Data JPA 1.10.3

Status

Assignee

Steve Ebersole

Reporter

Patrik Bakken

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.0.11

Priority

Major