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

XML Mapped Entity with a ManyToOne association to a Annotation Mapped Entity -> NullPointerException

Description

We're upgrading from Hibernate 4.3.11 to Hibernate 5.2.7 and are getting a NullPointerException in ModelBinder when it tries to access a PrimaryKey. It's null.

We have a mix of HBM and Annotation entities. It happens when an entity is mapped via XML and references an entity that is mapped via annotations - many to one. We're using the legacy bootstrapping process via spring's LocalSessionFactoryBuilder (version 5). I've tried changing to non-legacy native bootstrapping, but that made no difference.

Here's the exception:

NPE in ModelBinder

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Caused by: java.lang.NullPointerException at org.hibernate.boot.model.source.internal.hbm.ModelBinder$ManyToOneColumnBinder.doSecondPass(ModelBinder.java:4140) ~[hibernate-core-5.2.7.Final.jar:5.2.7.Final] at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1621) ~[hibernate-core-5.2.7.Final.jar:5.2.7.Final] at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1576) ~[hibernate-core-5.2.7.Final.jar:5.2.7.Final] at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278) ~[hibernate-core-5.2.7.Final.jar:5.2.7.Final] at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83) ~[hibernate-core-5.2.7.Final.jar:5.2.7.Final] at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418) ~[hibernate-core-5.2.7.Final.jar:5.2.7.Final] at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87) ~[hibernate-core-5.2.7.Final.jar:5.2.7.Final] at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691) ~[hibernate-core-5.2.7.Final.jar:5.2.7.Final] at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726) ~[hibernate-core-5.2.7.Final.jar:5.2.7.Final] at com.i2rd.hibernate.util.RebuildableSessionFactoryBean.createSessionFactory(RebuildableSessionFactoryBean.java:242) ~[net.proteusframework-core.jar:?] at com.i2rd.hibernate.util.RebuildableSessionFactoryBean.rebuildSessionFactory(RebuildableSessionFactoryBean.java:204) ~[net.proteusframework-core.jar:?] at com.i2rd.hibernate.util.RebuildableSessionFactoryBean.getObject(RebuildableSessionFactoryBean.java:182) ~[net.proteusframework-core.jar:?] at com.i2rd.hibernate.util.RebuildableSessionFactoryBean.getObject(RebuildableSessionFactoryBean.java:54) ~[net.proteusframework-core.jar:?] at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]

Originally posted here: https://forum.hibernate.org/viewtopic.php?f=1&t=1044033

I worked around it in the short-term by changing ModelBinder to create the primary key on-demand if it doesn't exist. https://github.com/rtennantventuretech/hibernate-orm/commit/c2bf4b808259c8a56bc494ece7a4ba50874793ec

Test case: https://github.com/hibernate/hibernate-orm/pull/1796

Environment

Hibernate 5.2.8
PostgreSQL 9.5.5

Status

Assignee

Chris Cranford

Reporter

RussT

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Worked in

4.3.11

Components

Affects versions

5.2.8

Priority

Major