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

NullPointerException after persisting null NaturalId-values in combination with @NaturalIdCache

Description

Since Steve has fixed (see org.hibernate.test.naturalid.nullable.NullableNaturalIdTest),
there's no NullPointerException anymore when persisting null values.
But a NullPointerException raises again when annotating the entity class with @NaturalIdCache.

Cause:

NaturalIdCacheKey.java constructor

1 result = prime * result + type.getHashCode( value, factory ); // raises NPE when value is null

Proposed fix:

NaturalIdCacheKey.java constructor

1 result = prime * result + (value == null ? 0 : type.getHashCode( value, factory ));

Testcase (see attachement):

org.hibernate.test.annotations.naturalid.NaturalIdOnSingleManyToOneTest.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 @Test public void testManyToOnePersistMutableNaturalIdWithNullValue() { NaturalIdOnManyToOne singleManyToOne = new NaturalIdOnManyToOne(); Citizen c1 = new Citizen(); c1.setFirstname( "Emmanuel" ); c1.setLastname( "Bernard" ); c1.setSsn( "1234" ); State france = new State(); france.setName( "Ile de France" ); c1.setState( null ); singleManyToOne.setCitizen( c1 ); Session s = openSession(); Transaction tx = s.beginTransaction(); s.persist( france ); s.persist( c1 ); s.persist( singleManyToOne ); c1.setState( france ); s.flush(); // --> java.lang.NullPointerException below raises, test passes when removing @NaturalIdCache annotation on Citizen entity

Environment

None

Status

Assignee

Unassigned

Reporter

Guenther Demetz

Fix versions

None

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Affects versions

4.1.1

Priority

Minor