[hibernate-issues] [Hibernate-JIRA] Created: (HHH-7177) NullPointerException after persisting null NaturalId-values in combination with @NaturalIdCache

Guenther Demetz (JIRA) noreply at atlassian.com
Thu Mar 15 09:57:48 EDT 2012


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

                 Key: HHH-7177
                 URL: https://hibernate.onjira.com/browse/HHH-7177
             Project: Hibernate ORM
          Issue Type: Bug
          Components: caching (L2)
    Affects Versions: 4.1.1
            Reporter: Guenther Demetz
            Priority: Minor


Since Steve has fixed HHH-7158 (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:
{code:title=NaturalIdCacheKey.java constructor|borderStyle=solid}
result = prime * result + type.getHashCode( value, factory ); // raises NPE when value is null
{code}

Proposed fix:
{code:title=NaturalIdCacheKey.java constructor|borderStyle=solid}
result = prime * result + (value == null ? 0 : type.getHashCode( value, factory )); 
{code}

Testcase (see attachement):
{code:title=org.hibernate.test.annotations.naturalid.NaturalIdOnSingleManyToOneTest.java|borderStyle=solid}
        @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 
{code}


--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list