diff --git a/hibernate-core/src/test/java/org/hibernate/test/cache/polymorphism/AbstractCachedItem.java b/hibernate-core/src/test/java/org/hibernate/test/cache/polymorphism/AbstractCachedItem.java new file mode 100644 index 0000000000..efec2b1ec8 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/cache/polymorphism/AbstractCachedItem.java @@ -0,0 +1,68 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010-2014, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.test.cache.polymorphism; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +import org.hibernate.annotations.Cache; +import org.hibernate.annotations.CacheConcurrencyStrategy; +import org.hibernate.annotations.GenericGenerator; + +/** + * @author Steve Ebersole + * @author Guillaume Smet + */ +@Entity +@Cache(usage = CacheConcurrencyStrategy.READ_WRITE) +public class AbstractCachedItem { + private Long id; + private String name; + + public AbstractCachedItem() { + } + + public AbstractCachedItem(String name) { + this.name = name; + } + + @Id + @GeneratedValue(generator = "increment") + @GenericGenerator(name = "increment", strategy = "increment") + 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; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/cache/polymorphism/CachedItem1.java b/hibernate-core/src/test/java/org/hibernate/test/cache/polymorphism/CachedItem1.java new file mode 100644 index 0000000000..1be8d7a9af --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/cache/polymorphism/CachedItem1.java @@ -0,0 +1,43 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2014, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.test.cache.polymorphism; + +import javax.persistence.Entity; + + +/** + * @author Guillaume Smet + */ +@Entity +public class CachedItem1 extends AbstractCachedItem { + + public CachedItem1() { + super(); + } + + public CachedItem1(String name) { + super( name ); + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/cache/polymorphism/CachedItem2.java b/hibernate-core/src/test/java/org/hibernate/test/cache/polymorphism/CachedItem2.java new file mode 100644 index 0000000000..95b579f9e2 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/cache/polymorphism/CachedItem2.java @@ -0,0 +1,43 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2014, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.test.cache.polymorphism; + +import javax.persistence.Entity; + + +/** + * @author Guillaume Smet + */ +@Entity +public class CachedItem2 extends AbstractCachedItem { + + public CachedItem2() { + super(); + } + + public CachedItem2(String name) { + super( name ); + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/cache/polymorphism/PolymorphicCacheTest.java b/hibernate-core/src/test/java/org/hibernate/test/cache/polymorphism/PolymorphicCacheTest.java new file mode 100644 index 0000000000..bf0ce9dbd7 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/cache/polymorphism/PolymorphicCacheTest.java @@ -0,0 +1,81 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2012-2014, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.test.cache.polymorphism; + +import static org.junit.Assert.assertNull; + +import org.hibernate.Session; +import org.hibernate.cfg.Configuration; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +/** + * @author Guillaume Smet + */ +@TestForIssue(jiraKey = "HHH-9028") +public class PolymorphicCacheTest extends BaseCoreFunctionalTestCase { + @Override + protected void configure(Configuration configuration) { + super.configure( configuration ); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { AbstractCachedItem.class, CachedItem1.class, CachedItem2.class }; + } + + @Test + public void testPolymorphismAndCache() throws Exception { + final CachedItem1 cachedItem1 = new CachedItem1( "name 1" ); + final CachedItem2 cachedItem2 = new CachedItem2( "name 2" ); + + // create the 2 items + Session s = openSession(); + s.beginTransaction(); + s.save( cachedItem1 ); + s.save( cachedItem2 ); + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.beginTransaction(); + // As the first item is supposed to be a CachedItem1, it shouldn't be returned. + // Note that the Session API is not type safe but, when using the EntityManager.find API, you get a ClassCastException + // if calling find returns the object. + Object thisObjectShouldBeNull = s.get( CachedItem2.class, cachedItem1.getId() ); + assertNull( thisObjectShouldBeNull ); + s.getTransaction().commit(); + s.close(); + + // cleanup + s = openSession(); + s.beginTransaction(); + s.delete( cachedItem1 ); + s.delete( cachedItem2 ); + s.getTransaction().commit(); + s.close(); + } + +}