When using the @Id, @OneToOne and @PrimaryKeyJoinColumn annotations on a property, MetadataBuilder.build() throws a NullPointerException from the following code path:
java.lang.NullPointerException
at org.hibernate.mapping.PersistentClass.createPrimaryKey(PersistentClass.java:363)
at org.hibernate.cfg.CreateKeySecondPass.doSecondPass(CreateKeySecondPass.java:31)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1621)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1585)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
I could reproduce this issue using both Hibernate ORM 5.1.3 and 5.2.6. According to the Java EE docs, the @PrimaryKeyJoinColumn annotation may be used in a OneToOne mapping in which the primary key of the referencing entity is used as a foreign key to the referenced entity. It seems like RootClass.identifier is null during the second pass (see above), when reached from the following code path:
at org.hibernate.mapping.RootClass.getIdentifier(RootClass.java:94)
at org.hibernate.mapping.RootClass.getKey(RootClass.java:207)
at org.hibernate.mapping.PersistentClass.createPrimaryKey(PersistentClass.java:363)
A self-contained demo is available in HHH-11371.zip, it's runnable with mvn clean test. The @PrimaryKeyJoinColumn annotation is present on the Child.Parent field, the Hibernate bootstrapping process is attempted in Main.main(). The @OneToOne annotation is not bidirectional, it only goes from the Child to the Parent. |