diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/Caching.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/Caching.java index 2d0b17eb8e..ef1838d6d4 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/Caching.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/Caching.java @@ -24,7 +24,7 @@ package org.hibernate.metamodel.binding; /** - * TODO : javadoc + * Defines the caching settings for an entity. * * @author Steve Ebersole */ diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/EntityBinder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/EntityBinder.java index 604df500a9..bc98115c5a 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/EntityBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/EntityBinder.java @@ -28,11 +28,14 @@ import java.util.List; import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationValue; +import org.hibernate.AnnotationException; import org.hibernate.AssertionFailure; import org.hibernate.MappingException; +import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.OptimisticLockType; import org.hibernate.annotations.PolymorphismType; import org.hibernate.internal.util.StringHelper; +import org.hibernate.metamodel.binding.Caching; import org.hibernate.metamodel.binding.EntityBinding; import org.hibernate.metamodel.binding.SimpleAttributeBinding; import org.hibernate.metamodel.domain.Entity; @@ -67,6 +70,7 @@ public class EntityBinder { bindJpaEntityAnnotation( entityBinding ); bindHibernateEntityAnnotation( entityBinding ); // optional hibernate specific @org.hibernate.annotations.Entity bindWhereFilter( entityBinding ); + bindCaching( entityBinding ); schemaName = createSchemaName(); bindTable( entityBinding ); @@ -89,6 +93,41 @@ public class EntityBinder { } } + private void bindCaching(EntityBinding entityBinding) { + AnnotationInstance cacheAnnotation = JandexHelper.getSingleAnnotation( + configuredClass.getClassInfo(), HibernateDotNames.CACHE + ); + if ( cacheAnnotation != null ) { + String region; + if ( cacheAnnotation.value( "region" ) != null ) { + region = cacheAnnotation.value( "region" ).asString(); + } + else { + region = entityBinding.getEntity().getName(); + } + + boolean cacheLazyProperties = true; + if ( cacheAnnotation.value( "include" ) != null ) { + String tmp = cacheAnnotation.value( "include" ).asString(); + if ( "all".equalsIgnoreCase( tmp ) ) { + cacheLazyProperties = true; + } + else if ( "non-lazy".equalsIgnoreCase( tmp ) ) { + cacheLazyProperties = false; + } + else { + throw new AnnotationException( "Unknown lazy property annotations: " + tmp ); + } + } + + CacheConcurrencyStrategy strategy = CacheConcurrencyStrategy.valueOf( + cacheAnnotation.value( "usage" ).asEnum() + ); + Caching caching = new Caching( region, strategy.toAccessType().getExternalName(), cacheLazyProperties ); + entityBinding.setCaching( caching ); + } + } + private Schema.Name createSchemaName() { String schema = null; String catalog = null; diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/CacheBindingTests.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/CacheBindingTests.java new file mode 100644 index 0000000000..f24facb429 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/CacheBindingTests.java @@ -0,0 +1,105 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010, 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.metamodel.source.annotations; + +import javax.persistence.Cacheable; +import javax.persistence.Entity; +import javax.persistence.Id; + +import org.junit.Test; + +import org.hibernate.annotations.Cache; +import org.hibernate.annotations.CacheConcurrencyStrategy; +import org.hibernate.metamodel.MetadataSources; +import org.hibernate.metamodel.binding.Caching; +import org.hibernate.metamodel.binding.EntityBinding; +import org.hibernate.metamodel.source.internal.MetadataImpl; +import org.hibernate.service.ServiceRegistryBuilder; +import org.hibernate.testing.FailureExpected; +import org.hibernate.testing.junit4.BaseUnitTestCase; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertNull; + +/** + * Tests for {@code o.h.a.Cache} and {@code j.p.Cacheable}. + * + * @author Hardy Ferentschik + */ +public class CacheBindingTests extends BaseUnitTestCase { + @Test + public void testHibernateCaching() { + EntityBinding binding = getEntityBinding( HibernateCacheEntity.class ); + assertNotNull( "There should be a cache binding", binding.getCaching() ); + Caching caching = binding.getCaching(); + assertEquals( "Wrong region", "foo", caching.getRegion() ); + assertEquals( "Wrong strategy", "read-write", caching.getStrategy() ); + assertEquals( "Wrong lazy properties configuration", false, caching.isCacheLazyProperties() ); + } + + @Test + @FailureExpected( jiraKey = "HHH-6207", message = "under construction") + public void testJpaCaching() { + EntityBinding binding = getEntityBinding( JpaCacheEntity.class ); + assertNotNull( "There should be a cache binding", binding.getCaching() ); + } + + @Test + public void testNoCaching() { + EntityBinding binding = getEntityBinding( NoCacheEntity.class ); + assertNull( "There should be no cache binding", binding.getCaching() ); + } + + private EntityBinding getEntityBinding(Class clazz) { + MetadataSources sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() ); + sources.addAnnotatedClass( clazz ); + MetadataImpl metadata = (MetadataImpl) sources.buildMetadata(); + + return metadata.getEntityBinding( this.getClass().getSimpleName() + "$" + clazz.getSimpleName() ); + } + + @Entity + @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "foo", include = "non-lazy") + class HibernateCacheEntity { + @Id + private int id; + } + + @Entity + @Cacheable + class JpaCacheEntity { + @Id + private int id; + } + + @Entity + @Cacheable + class NoCacheEntity { + @Id + private int id; + } +} + +