diff --git a/hibernate-ehcache/src/test/java/org/hibernate/cache/ehcache/test/IdentityIdentifierDelayedInsertTest.java b/hibernate-ehcache/src/test/java/org/hibernate/cache/ehcache/test/IdentityIdentifierDelayedInsertTest.java index 6bbe8e02e9..897d7bea90 100644 --- a/hibernate-ehcache/src/test/java/org/hibernate/cache/ehcache/test/IdentityIdentifierDelayedInsertTest.java +++ b/hibernate-ehcache/src/test/java/org/hibernate/cache/ehcache/test/IdentityIdentifierDelayedInsertTest.java @@ -13,6 +13,8 @@ import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; import org.hibernate.FlushMode; import org.hibernate.annotations.Cache; @@ -43,13 +45,109 @@ public class IdentityIdentifierDelayedInsertTest extends BaseNonConfigCoreFuncti settings.put( AvailableSettings.GENERATE_STATISTICS, "false" ); } + @Entity(name = "NonCachedEntity") + public static class NonCachedEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private String data; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( o == null || getClass() != o.getClass() ) { + return false; + } + NonCachedEntity that = (NonCachedEntity) o; + return Objects.equals( id, that.id ) && + Objects.equals( data, that.data ); + } + + @Override + public int hashCode() { + return Objects.hash( id, data ); + } + } + + @Entity(name = "CachedEntity") + @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) + public static class CachedEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private String name; + @ManyToOne + @JoinColumn(nullable = false) + private NonCachedEntity nonCachedEntity; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public NonCachedEntity getNonCachedEntity() { + return nonCachedEntity; + } + + public void setNonCachedEntity(NonCachedEntity nonCachedEntity) { + this.nonCachedEntity = nonCachedEntity; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( o == null || getClass() != o.getClass() ) { + return false; + } + CachedEntity that = (CachedEntity) o; + return Objects.equals( id, that.id ) && + Objects.equals( name, that.name ) && + Objects.equals( nonCachedEntity, that.nonCachedEntity ); + } + + @Override + public int hashCode() { + return Objects.hash( id, name, nonCachedEntity ); + } + } + @Entity(name = "SomeEntity") @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) public static class SomeEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - private String name; public Long getId() { @@ -96,7 +194,7 @@ public class IdentityIdentifierDelayedInsertTest extends BaseNonConfigCoreFuncti @Override protected Class[] getAnnotatedClasses() { - return new Class[] { SomeEntity.class }; + return new Class[] { SomeEntity.class, NonCachedEntity.class, CachedEntity.class }; } @Test @@ -113,4 +211,19 @@ public class IdentityIdentifierDelayedInsertTest extends BaseNonConfigCoreFuncti session.clear(); } ); } + + @Test + @TestForIssue(jiraKey = "HHH-13164") + public void testPersistingCachedEntityWithIdentityBasedIdentifierReferencingNonCachedEntity() { + doInHibernate( this::sessionFactory, session -> { + final NonCachedEntity nonCachedEntity = new NonCachedEntity(); + nonCachedEntity.setData( "NonCachedEntity" ); + session.persist( nonCachedEntity ); + + final CachedEntity cachedEntity = new CachedEntity(); + cachedEntity.setName( "CachedEntity" ); + cachedEntity.setNonCachedEntity( nonCachedEntity ); + session.persist( cachedEntity ); + } ); + } }