From f5e6a44f7e8eb8f75092230f566a29c4c4e78da6 Mon Sep 17 00:00:00 2001 From: Guenther Demetz Date: Thu, 26 Apr 2012 16:00:30 +0200 Subject: [PATCH] HHH-7279 --- .../internal/StatefulPersistenceContext.java | 3 ++ .../naturalid/mutable/cached/Another.java | 9 +++++ .../cached/CachedMutableNaturalIdTest.java | 37 +++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java b/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java index b5a38b2021..2282a994a6 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java @@ -1801,6 +1801,9 @@ public class StatefulPersistenceContext implements PersistenceContext { CachedNaturalIdValueSource source) { final NaturalIdRegionAccessStrategy naturalIdCacheAccessStrategy = persister.getNaturalIdCacheAccessStrategy(); final NaturalIdCacheKey naturalIdCacheKey = new NaturalIdCacheKey( naturalIdValues, persister, session ); + if (naturalIdCacheAccessStrategy.get(naturalIdCacheKey, session.getTimestamp()) != null) { + return; // prevent identical re-cachings + } final SessionFactoryImplementor factory = session.getFactory(); diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/naturalid/mutable/cached/Another.java b/hibernate-core/src/matrix/java/org/hibernate/test/naturalid/mutable/cached/Another.java index 21150620c5..32c3af4fad 100644 --- a/hibernate-core/src/matrix/java/org/hibernate/test/naturalid/mutable/cached/Another.java +++ b/hibernate-core/src/matrix/java/org/hibernate/test/naturalid/mutable/cached/Another.java @@ -39,6 +39,7 @@ import org.hibernate.annotations.NaturalIdCache; public class Another { private Integer id; private String name; + private String surname; public Another() { } @@ -66,4 +67,12 @@ public class Another { public void setName(String name) { this.name = name; } + + public String getSurname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } } diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/naturalid/mutable/cached/CachedMutableNaturalIdTest.java b/hibernate-core/src/matrix/java/org/hibernate/test/naturalid/mutable/cached/CachedMutableNaturalIdTest.java index c08f8c3287..3ec8b048a1 100755 --- a/hibernate-core/src/matrix/java/org/hibernate/test/naturalid/mutable/cached/CachedMutableNaturalIdTest.java +++ b/hibernate-core/src/matrix/java/org/hibernate/test/naturalid/mutable/cached/CachedMutableNaturalIdTest.java @@ -23,6 +23,8 @@ */ package org.hibernate.test.naturalid.mutable.cached; +import java.io.Serializable; + import org.junit.Test; import org.hibernate.Session; @@ -225,5 +227,40 @@ public class CachedMutableNaturalIdTest extends BaseCoreFunctionalTestCase { assertEquals(0, session.getSessionFactory().getStatistics().getNaturalIdCacheHitCount()); } + + @Test + public void testNaturalIdRecachingWhenNeeded() { + Session session = openSession(); + session.getSessionFactory().getStatistics().clear(); + session.beginTransaction(); + Another it = new Another( "it"); + session.save( it ); + Serializable id = it.getId(); + session.getTransaction().commit(); + session.close(); + + session = openSession(); + for (int i=0; i < 10; i++) { + session.beginTransaction(); + it = (Another) session.byId(Another.class).load(id); + if (i == 9) { + it.setName("name" + i); + } + it.setSurname("surname" + i); // changing something but not the natural-id's + session.getTransaction().commit(); + } + + session = openSession(); + session.beginTransaction(); + it = (Another) session.bySimpleNaturalId(Another.class).load("it"); + assertNull(it); + assertEquals(0, session.getSessionFactory().getStatistics().getNaturalIdCacheHitCount()); + it = (Another) session.byId(Another.class).load(id); + session.delete(it); + session.getTransaction().commit(); + + // finally there should be only 2 NaturalIdCache puts : 1. insertion, 2. when updating natural-id from 'it' to 'name9' + assertEquals(2, session.getSessionFactory().getStatistics().getNaturalIdCachePutCount()); + } }