|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.cache.ehcache.test; |
| 8 | + |
| 9 | +import java.util.Map; |
| 10 | +import java.util.Objects; |
| 11 | + |
| 12 | +import javax.persistence.Entity; |
| 13 | +import javax.persistence.GeneratedValue; |
| 14 | +import javax.persistence.GenerationType; |
| 15 | +import javax.persistence.Id; |
| 16 | + |
| 17 | +import org.hibernate.FlushMode; |
| 18 | +import org.hibernate.annotations.Cache; |
| 19 | +import org.hibernate.annotations.CacheConcurrencyStrategy; |
| 20 | +import org.hibernate.cache.ehcache.internal.SingletonEhcacheRegionFactory; |
| 21 | +import org.hibernate.cfg.AvailableSettings; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import org.hibernate.testing.TestForIssue; |
| 25 | +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; |
| 26 | + |
| 27 | +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; |
| 28 | + |
| 29 | +/** |
| 30 | + * @author Chris Cranford |
| 31 | + */ |
| 32 | +public class IdentityIdentifierDelayedInsertTest extends BaseNonConfigCoreFunctionalTestCase { |
| 33 | + @Override |
| 34 | + protected void addSettings(Map settings) { |
| 35 | + super.addSettings( settings ); |
| 36 | + |
| 37 | + settings.put( AvailableSettings.FLUSH_MODE, FlushMode.COMMIT ); |
| 38 | + settings.put( AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "false" ); |
| 39 | + settings.put( AvailableSettings.USE_SECOND_LEVEL_CACHE, "true" ); |
| 40 | + settings.put( AvailableSettings.USE_QUERY_CACHE, "false" ); |
| 41 | + settings.put( AvailableSettings.AUTO_EVICT_COLLECTION_CACHE, "true" ); |
| 42 | + settings.put( AvailableSettings.CACHE_REGION_FACTORY, SingletonEhcacheRegionFactory.class.getName() ); |
| 43 | + settings.put( AvailableSettings.GENERATE_STATISTICS, "false" ); |
| 44 | + } |
| 45 | + |
| 46 | + @Entity(name = "SomeEntity") |
| 47 | + @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) |
| 48 | + public static class SomeEntity { |
| 49 | + @Id |
| 50 | + @GeneratedValue(strategy = GenerationType.IDENTITY) |
| 51 | + private Long id; |
| 52 | + |
| 53 | + private String name; |
| 54 | + |
| 55 | + public Long getId() { |
| 56 | + return id; |
| 57 | + } |
| 58 | + |
| 59 | + public void setId(Long id) { |
| 60 | + this.id = id; |
| 61 | + } |
| 62 | + |
| 63 | + public String getName() { |
| 64 | + return name; |
| 65 | + } |
| 66 | + |
| 67 | + public void setName(String name) { |
| 68 | + this.name = name; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public final boolean equals(Object o) { |
| 73 | + if (this == o) { |
| 74 | + return true; |
| 75 | + } |
| 76 | + if (o == null || getClass() != o.getClass()) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + SomeEntity asset = (SomeEntity) o; |
| 80 | + if (asset.id == null || id == null) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + return Objects.equals(id, asset.id); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public final int hashCode() { |
| 88 | + return Objects.hashCode(id); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public String toString() { |
| 93 | + return "SomeEntity{" + "id=" + id + ", name='" + name + '\'' + '}'; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + protected Class[] getAnnotatedClasses() { |
| 99 | + return new Class<?>[] { SomeEntity.class }; |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + @TestForIssue(jiraKey = "HHH-13147") |
| 104 | + public void testPersistingCachedEntityWithIdentityBasedIdentifier() { |
| 105 | + doInHibernate( this::sessionFactory, session -> { |
| 106 | + SomeEntity entity = new SomeEntity(); |
| 107 | + session.persist( entity ); |
| 108 | + |
| 109 | + entity.setName( "foo" ); |
| 110 | + session.persist( entity ); |
| 111 | + |
| 112 | + session.flush(); |
| 113 | + session.clear(); |
| 114 | + } ); |
| 115 | + } |
| 116 | +} |
0 commit comments