diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/EnhancerTest.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/EnhancerTest.java index 14276c5813..c3bb6a2d45 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/EnhancerTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/EnhancerTest.java @@ -27,6 +27,7 @@ import org.hibernate.test.bytecode.enhancement.lazy.LazyLoadingIntegrationTestTa import org.hibernate.test.bytecode.enhancement.lazy.LazyLoadingTestTask; import org.hibernate.test.bytecode.enhancement.lazy.basic.LazyBasicFieldAccessTestTask; import org.hibernate.test.bytecode.enhancement.lazy.basic.LazyBasicPropertyAccessTestTask; +import org.hibernate.test.bytecode.enhancement.lazyCache.InitFromCacheTestTask; import org.hibernate.test.bytecode.enhancement.merge.CompositeMergeTestTask; import org.hibernate.test.bytecode.enhancement.pk.EmbeddedPKTestTask; import org.hibernate.test.bytecode.enhancement.ondemandload.LazyCollectionWithClearedSessionTestTask; @@ -123,4 +124,9 @@ public class EnhancerTest extends BaseUnitTestCase { public void testLazyBasicFieldNotInitialized() { EnhancerTestUtils.runEnhancerTestTask( LazyBasicFieldNotInitializedTestTask.class ); } + + @Test + public void testInitFromCache() { + EnhancerTestUtils.runEnhancerTestTask( InitFromCacheTestTask.class ); + } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazyCache/AbstractCachingTestTask.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazyCache/AbstractCachingTestTask.java new file mode 100644 index 0000000000..db3db5022a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazyCache/AbstractCachingTestTask.java @@ -0,0 +1,54 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.bytecode.enhancement.lazyCache; + +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl; +import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.engine.spi.SessionFactoryImplementor; + +import org.hibernate.testing.bytecode.enhancement.EnhancerTestTask; + +/** + * @author Steve Ebersole + */ +public abstract class AbstractCachingTestTask implements EnhancerTestTask { + private SessionFactoryImplementor sessionFactory; + + protected SessionFactoryImplementor sessionFactory() { + return sessionFactory; + } + + @Override + public Class[] getAnnotatedClasses() { + return new Class[] { Document.class }; + } + + @Override + public void prepare() { + StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder(); + registryBuilder.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); + registryBuilder.applySetting( AvailableSettings.HBM2DDL_AUTO, "create-drop" ); + registryBuilder.addService( ClassLoaderService.class, new ClassLoaderServiceImpl( Thread.currentThread().getContextClassLoader() ) ); + StandardServiceRegistry registry = registryBuilder.build(); + + MetadataSources metadataSources = new MetadataSources( registry ); + metadataSources.addAnnotatedClass( Document.class ); + + sessionFactory = (SessionFactoryImplementor) metadataSources.buildMetadata().buildSessionFactory(); + } + + @Override + public void complete() { + if ( sessionFactory != null ) { + sessionFactory.close(); + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/lazycache/Document.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazyCache/Document.java similarity index 63% rename from hibernate-core/src/test/java/org/hibernate/test/lazycache/Document.java rename to hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazyCache/Document.java index 506810f91c..e27cf9cb9c 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/lazycache/Document.java +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazyCache/Document.java @@ -4,104 +4,102 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ +package org.hibernate.test.bytecode.enhancement.lazyCache; -//$Id: Document.java 7772 2005-08-05 23:03:46Z oneovthafew $ -package org.hibernate.test.lazycache; import java.util.Date; import java.util.Locale; +import javax.persistence.Basic; +import javax.persistence.Cacheable; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +import org.hibernate.annotations.Cache; +import org.hibernate.annotations.CacheConcurrencyStrategy; +import org.hibernate.annotations.Formula; /** * @author Gavin King */ +@Entity +@Cacheable +@Cache( usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, include = "non-lazy", region = "foo" ) public class Document { - + @Id + @GeneratedValue private Long id; private String name; + @Basic( fetch = FetchType.LAZY ) + @Formula( "upper(name)" ) private String upperCaseName; + @Basic( fetch = FetchType.LAZY ) private String summary; + @Basic( fetch = FetchType.LAZY ) private String text; + @Basic( fetch = FetchType.LAZY ) private Date lastTextModification; + + Document() { + } public Document(String name, String summary, String text) { - lastTextModification = new Date(); + this.lastTextModification = new Date(); this.name = name; - upperCaseName = name.toUpperCase(Locale.ROOT); + this.upperCaseName = name.toUpperCase( Locale.ROOT ); this.summary = summary; this.text = text; } - - Document() {} - - public Date getLastTextModification() { - return lastTextModification; - } - /** - * @return Returns the id. - */ public Long getId() { return id; } - /** - * @param id The id to set. - */ + public void setId(Long id) { this.id = id; } - /** - * @return Returns the name. - */ + public String getName() { return name; } - /** - * @param name The name to set. - */ + public void setName(String name) { this.name = name; } - /** - * @return Returns the summary. - */ - public String getSummary() { - return summary; - } - /** - * @param summary The summary to set. - */ - public void setSummary(String summary) { - this.summary = summary; - } - /** - * @return Returns the text. - */ - public String getText() { - return text; - } - /** - * @param text The text to set. - */ - private void setText(String text) { - this.text = text; - } - /** - * @return Returns the upperCaseName. - */ + public String getUpperCaseName() { return upperCaseName; } - /** - * @param upperCaseName The upperCaseName to set. - */ + public void setUpperCaseName(String upperCaseName) { this.upperCaseName = upperCaseName; } - + + public String getSummary() { + return summary; + } + + public void setSummary(String summary) { + this.summary = summary; + } + + public String getText() { + return text; + } + + private void setText(String text) { + this.text = text; + } + public void updateText(String newText) { if ( !newText.equals(text) ) { this.text = newText; lastTextModification = new Date(); } } + + public Date getLastTextModification() { + return lastTextModification; + } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazyCache/InitFromCacheTestTask.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazyCache/InitFromCacheTestTask.java new file mode 100644 index 0000000000..227bff614e --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazyCache/InitFromCacheTestTask.java @@ -0,0 +1,61 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.bytecode.enhancement.lazyCache; + +import org.hibernate.Hibernate; +import org.hibernate.Session; +import org.hibernate.Transaction; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * @author Steve Ebersole + */ +public class InitFromCacheTestTask extends AbstractCachingTestTask { + @Override + public void execute() { + Session s; + Transaction tx; + + s = sessionFactory().openSession(); + tx = s.beginTransaction(); + s.persist( new Document("HiA", "Hibernate book", "Hibernate is....") ); + tx.commit(); + s.close(); + + s = sessionFactory().openSession(); + tx = s.beginTransaction(); + s.createQuery("from Document fetch all properties").uniqueResult(); + tx.commit(); + s.close(); + + sessionFactory().getStatistics().clear(); + + s = sessionFactory().openSession(); + tx = s.beginTransaction(); + Document d = (Document) s.createCriteria(Document.class).uniqueResult(); + assertFalse( Hibernate.isPropertyInitialized( d, "text") ); + assertFalse( Hibernate.isPropertyInitialized(d, "summary") ); + assertEquals( "Hibernate is....", d.getText() ); + assertTrue( Hibernate.isPropertyInitialized(d, "text") ); + assertTrue( Hibernate.isPropertyInitialized(d, "summary") ); + tx.commit(); + s.close(); + + assertEquals( 2, sessionFactory().getStatistics().getPrepareStatementCount() ); + + s = sessionFactory().openSession(); + tx = s.beginTransaction(); + d = s.get(Document.class, d.getId()); + assertFalse( Hibernate.isPropertyInitialized(d, "text") ); + assertFalse( Hibernate.isPropertyInitialized(d, "summary") ); + tx.commit(); + s.close(); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/lazycache/Documents.hbm.xml b/hibernate-core/src/test/java/org/hibernate/test/lazycache/Documents.hbm.xml deleted file mode 100755 index e604ca5757..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/lazycache/Documents.hbm.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - diff --git a/hibernate-core/src/test/java/org/hibernate/test/lazycache/InstrumentCacheTest.java b/hibernate-core/src/test/java/org/hibernate/test/lazycache/InstrumentCacheTest.java deleted file mode 100755 index 728a76ab66..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/lazycache/InstrumentCacheTest.java +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.lazycache; - -import org.junit.Test; - -import org.hibernate.Hibernate; -import org.hibernate.Session; -import org.hibernate.Transaction; -import org.hibernate.cfg.Configuration; -import org.hibernate.cfg.Environment; -import org.hibernate.engine.spi.PersistentAttributeInterceptable; - -import org.hibernate.testing.Skip; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -/** - * @author Gavin King - */ -@Skip( condition = InstrumentCacheTest.SkipMatcher.class, message = "Test domain classes not instrumented" ) -public class InstrumentCacheTest extends BaseCoreFunctionalTestCase { - public static class SkipMatcher implements Skip.Matcher { - @Override - public boolean isMatch() { - return ! PersistentAttributeInterceptable.class.isAssignableFrom( Document.class ); - } - } - - public String[] getMappings() { - return new String[] { "lazycache/Documents.hbm.xml" }; - } - - public void configure(Configuration cfg) { - cfg.setProperty(Environment.GENERATE_STATISTICS, "true"); - } - - public boolean overrideCacheStrategy() { - return false; - } - - @Test - public void testInitFromCache() { - Session s; - Transaction tx; - - s = sessionFactory().openSession(); - tx = s.beginTransaction(); - s.persist( new Document("HiA", "Hibernate book", "Hibernate is....") ); - tx.commit(); - s.close(); - - s = sessionFactory().openSession(); - tx = s.beginTransaction(); - s.createQuery("from Document fetch all properties").uniqueResult(); - tx.commit(); - s.close(); - - sessionFactory().getStatistics().clear(); - - s = sessionFactory().openSession(); - tx = s.beginTransaction(); - Document d = (Document) s.createCriteria(Document.class).uniqueResult(); - assertFalse( Hibernate.isPropertyInitialized(d, "text") ); - assertFalse( Hibernate.isPropertyInitialized(d, "summary") ); - assertEquals( "Hibernate is....", d.getText() ); - assertTrue( Hibernate.isPropertyInitialized(d, "text") ); - assertTrue( Hibernate.isPropertyInitialized(d, "summary") ); - tx.commit(); - s.close(); - - assertEquals( 2, sessionFactory().getStatistics().getPrepareStatementCount() ); - - s = sessionFactory().openSession(); - tx = s.beginTransaction(); - d = (Document) s.get(Document.class, d.getId()); - assertFalse( Hibernate.isPropertyInitialized(d, "text") ); - assertFalse( Hibernate.isPropertyInitialized(d, "summary") ); - tx.commit(); - s.close(); - } - - @Test - public void testInitFromCache2() { - Session s; - Transaction tx; - - s = sessionFactory().openSession(); - tx = s.beginTransaction(); - s.persist( new Document("HiA", "Hibernate book", "Hibernate is....") ); - tx.commit(); - s.close(); - - s = sessionFactory().openSession(); - tx = s.beginTransaction(); - s.createQuery("from Document fetch all properties").uniqueResult(); - tx.commit(); - s.close(); - - sessionFactory().getStatistics().clear(); - - s = sessionFactory().openSession(); - tx = s.beginTransaction(); - Document d = (Document) s.createCriteria(Document.class).uniqueResult(); - assertFalse( Hibernate.isPropertyInitialized(d, "text") ); - assertFalse( Hibernate.isPropertyInitialized(d, "summary") ); - assertEquals( "Hibernate is....", d.getText() ); - assertTrue( Hibernate.isPropertyInitialized(d, "text") ); - assertTrue( Hibernate.isPropertyInitialized(d, "summary") ); - tx.commit(); - s.close(); - - assertEquals( 1, sessionFactory().getStatistics().getPrepareStatementCount() ); - - s = sessionFactory().openSession(); - tx = s.beginTransaction(); - d = (Document) s.get(Document.class, d.getId()); - assertTrue( Hibernate.isPropertyInitialized(d, "text") ); - assertTrue( Hibernate.isPropertyInitialized(d, "summary") ); - tx.commit(); - s.close(); - } - -} -