HHH-7279
This commit is contained in:
parent
94341fdc44
commit
f5e6a44f7e
|
@ -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();
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue