Property with @Id and @GeneratedValue(strategy=IDENTITY) brokes @NaturalIdCache
Description
Attachments
- 08 Apr 2013, 04:23 PM
duplicates
follows up on
Activity
Steve Ebersole October 28, 2015 at 3:25 AM
As part of verifying that this issue affects 5.0, please just set the "Affects version". Leave the "verify-affects-5.0" label and leave the issue in "Awaiting Response" status; these are critical for us to be able to track these verifications and triage them. Thanks.
Steve Ebersole October 27, 2015 at 7:16 PM
This bug report does not indicate that the reported issue affects version 5.x. Versions prior to 5.x are no longer maintained. It would be a great help to the Hibernate team and community for someone to verify that the reported issue still affects version 5.x. If so, please add the 5.x version that you verified with to the list of affected-versions and attach the (preferably SSCCE) test case you used to do the verification to the report; from there the issues will be looked at during our triage meetings.
For details, see http://in.relation.to/2015/10/27/great-jira-cleanup-2015/
Andrey Anisimov April 9, 2013 at 4:39 AM
Please fix grammar mistake in bug title. Can't find the way to do it myself.
Andrey Anisimov April 8, 2013 at 4:43 PM
Possibly related to that bug.
Andrey Anisimov April 8, 2013 at 4:23 PM
That little patch fixes the problem (am I missed something?).
The following test case demonstrates the issue.
EntityWithNaturalKey.java
import org.hibernate.annotations.NaturalId; import org.hibernate.annotations.NaturalIdCache; import javax.persistence.Cacheable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import static javax.persistence.GenerationType.IDENTITY; @Entity @Cacheable @NaturalIdCache public class EntityWithNaturalKey { @Id @GeneratedValue(strategy = IDENTITY) private long id; @NaturalId(mutable = false) private String name; protected EntityWithNaturalKey() { } public EntityWithNaturalKey(String name) { this.name = name; } public long getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
NaturalIdCacheTest.java
import org.hibernate.Session; import org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.test.jpa.AbstractJPATest; import org.hibernate.testing.TestForIssue; import org.junit.Assert; import org.junit.Test; public class NaturalIdCacheTest extends AbstractJPATest { @Override public String[] getMappings() { return NO_MAPPINGS; } @Override public void configure(Configuration cfg) { super.configure(cfg); cfg.addAnnotatedClass(EntityWithNaturalKey.class); cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true"); cfg.setProperty(Environment.USE_QUERY_CACHE, "true"); cfg.setProperty(Environment.GENERATE_STATISTICS, "true"); cfg.setProperty(Environment.CACHE_REGION_FACTORY, SingletonEhCacheRegionFactory.class.getName()); } @Test @TestForIssue(jiraKey = "???") public void testPutIntoNaturalIdCache_newlyCreatedIdentity() throws Exception { // Bug description: // Creating new entity with auto generated identity // puts new item with value = null into natural id // cache region, and mainly because of this it will // never be (re-)populated later, causing natural // id queries performing again and again. Session s = openSession(); s.beginTransaction(); EntityWithNaturalKey e = new EntityWithNaturalKey("test"); s.persist(e); s.getTransaction().commit(); s.close(); // Assert.assertEquals(0, sessionFactory().getStatistics().getNaturalIdQueryExecutionCount()); // it would be perfect but not necessary s = openSession(); s.beginTransaction(); e = (EntityWithNaturalKey) s.bySimpleNaturalId(EntityWithNaturalKey.class).load("test"); Assert.assertNotNull(e); s.getTransaction().commit(); s.close(); long queryCount = sessionFactory().getStatistics().getNaturalIdQueryExecutionCount(); // entity must be cached, no more queries bust be issued // since we didn't modify anything. s = openSession(); s.beginTransaction(); e = (EntityWithNaturalKey) s.bySimpleNaturalId(EntityWithNaturalKey.class).load("test"); Assert.assertNotNull(e); s.getTransaction().commit(); s.close(); Assert.assertEquals(queryCount, sessionFactory().getStatistics().getNaturalIdQueryExecutionCount()); } }