From 002779dffb6e5a0e54e771c7bef388cccba2e82d Mon Sep 17 00:00:00 2001 From: Hardy Ferentschik Date: Mon, 13 Jun 2011 16:06:33 +0200 Subject: [PATCH] HHH-6300 Adding support for @Synchronize annotation --- .../metamodel/binding/EntityBinding.java | 14 ++-- .../binding/state/EntityBindingState.java | 4 +- .../annotations/entity/EntityBinder.java | 14 ++++ .../state/binding/EntityBindingStateImpl.java | 13 +++- .../state/binding/HbmEntityBindingState.java | 12 +-- ...eTest.java => InheritanceBindingTest.java} | 2 +- .../entity/SynchronizeBindingTests.java | 75 +++++++++++++++++++ 7 files changed, 113 insertions(+), 21 deletions(-) rename hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/{InheritanceTypeTest.java => InheritanceBindingTest.java} (97%) create mode 100644 hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/SynchronizeBindingTests.java diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/EntityBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/EntityBinding.java index 18ed6e3589..d054f522be 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/EntityBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/EntityBinding.java @@ -23,10 +23,8 @@ */ package org.hibernate.metamodel.binding; -import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; @@ -86,7 +84,7 @@ public class EntityBinding { private CustomSQL customUpdate; private CustomSQL customDelete; - private List synchronizedTableNames; + private Set synchronizedTableNames = new HashSet(); public EntityBinding initialize(EntityBindingState state) { this.isRoot = state.isRoot(); @@ -318,13 +316,13 @@ public class EntityBinding { return isAbstract; } - protected void addSynchronizedTable(String tablename) { - if ( synchronizedTableNames == null ) { - synchronizedTableNames = new ArrayList(); - } - synchronizedTableNames.add( tablename ); + protected void addSynchronizedTable(String tableName) { + synchronizedTableNames.add( tableName ); } + public Set getSynchronizedTableNames() { + return synchronizedTableNames; + } // Custom SQL ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/EntityBindingState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/EntityBindingState.java index 0d5eb36893..83963a6859 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/EntityBindingState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/EntityBindingState.java @@ -23,7 +23,7 @@ */ package org.hibernate.metamodel.binding.state; -import java.util.List; +import java.util.Set; import org.hibernate.metamodel.binding.Caching; import org.hibernate.metamodel.binding.CustomSQL; @@ -74,5 +74,5 @@ public interface EntityBindingState { CustomSQL getCustomDelete(); - List getSynchronizedTableNames(); + Set getSynchronizedTableNames(); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/EntityBinder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/EntityBinder.java index 0262045e99..51c71d69ab 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/EntityBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/EntityBinder.java @@ -100,6 +100,7 @@ public class EntityBinder { bindJpaCaching( entityBindingState ); bindHibernateCaching( entityBindingState ); bindProxy( entityBindingState ); + bindSynchronize( entityBindingState ); // take care of the id, attributes and relations if ( configuredClass.isRoot() ) { @@ -259,6 +260,19 @@ public class EntityBinder { entityBindingState.setProxyInterfaceName( proxyInterfaceClass ); } + private void bindSynchronize(EntityBindingStateImpl entityBindingState) { + AnnotationInstance synchronizeAnnotation = JandexHelper.getSingleAnnotation( + configuredClass.getClassInfo(), HibernateDotNames.SYNCHRONIZE + ); + + if ( synchronizeAnnotation != null ) { + String[] tableNames = synchronizeAnnotation.value().asStringArray(); + for ( String tableName : tableNames ) { + entityBindingState.addSynchronizedTableName( tableName ); + } + } + } + private Caching createCachingForCacheableAnnotation(EntityBindingStateImpl entityBindingState) { String region = entityBindingState.getEntityName(); RegionFactory regionFactory = meta.getServiceRegistry().getService( RegionFactory.class ); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/binding/EntityBindingStateImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/binding/EntityBindingStateImpl.java index 37b2a97e02..538ab63333 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/binding/EntityBindingStateImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/binding/EntityBindingStateImpl.java @@ -23,7 +23,8 @@ */ package org.hibernate.metamodel.source.annotations.entity.state.binding; -import java.util.List; +import java.util.HashSet; +import java.util.Set; import org.hibernate.annotations.OptimisticLockType; import org.hibernate.metamodel.binding.Caching; @@ -67,11 +68,12 @@ public class EntityBindingStateImpl implements EntityBindingState { private CustomSQL customUpdate; private CustomSQL customDelete; - private List synchronizedTableNames; + private Set synchronizedTableNames; public EntityBindingStateImpl(ConfiguredClass configuredClass) { this.isRoot = configuredClass.isRoot(); this.inheritanceType = configuredClass.getInheritanceType(); + this.synchronizedTableNames = new HashSet(); // TODO: where do these values come from? this.metaAttributeContext = null; @@ -81,7 +83,6 @@ public class EntityBindingStateImpl implements EntityBindingState { this.customInsert = null; this.customUpdate = null; this.customDelete = null; - this.synchronizedTableNames = null; } public void setEntityName(String entityName) { @@ -136,6 +137,10 @@ public class EntityBindingStateImpl implements EntityBindingState { this.proxyInterfaceName = proxyInterfaceName; } + public void addSynchronizedTableName(String tableName) { + synchronizedTableNames.add( tableName ); + } + @Override public boolean isRoot() { return isRoot; @@ -238,7 +243,7 @@ public class EntityBindingStateImpl implements EntityBindingState { } @Override - public List getSynchronizedTableNames() { + public Set getSynchronizedTableNames() { return synchronizedTableNames; } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmEntityBindingState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmEntityBindingState.java index e1447f5889..f532b591a7 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmEntityBindingState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmEntityBindingState.java @@ -23,8 +23,8 @@ */ package org.hibernate.metamodel.source.hbm.state.binding; -import java.util.ArrayList; -import java.util.List; +import java.util.HashSet; +import java.util.Set; import org.hibernate.MappingException; import org.hibernate.cache.spi.access.AccessType; @@ -74,7 +74,7 @@ public class HbmEntityBindingState implements EntityBindingState { private final CustomSQL customUpdate; private final CustomSQL customDelete; - private final List synchronizedTableNames; + private final Set synchronizedTableNames; public HbmEntityBindingState( boolean isRoot, @@ -84,7 +84,7 @@ public class HbmEntityBindingState implements EntityBindingState { this.isRoot = isRoot; this.entityInheritanceType = inheritanceType; - this.caching = createCaching( entityClazz, bindingContext.extractEntityName( entityClazz ) ); + this.caching = createCaching( entityClazz, bindingContext.extractEntityName( entityClazz ) ); metaAttributeContext = HbmHelper.extractMetaAttributeContext( entityClazz.getMeta(), true, bindingContext.getMetaAttributeContext() @@ -150,7 +150,7 @@ public class HbmEntityBindingState implements EntityBindingState { } if ( entityClazz.getSynchronize() != null ) { - synchronizedTableNames = new ArrayList( entityClazz.getSynchronize().size() ); + synchronizedTableNames = new HashSet( entityClazz.getSynchronize().size() ); for ( XMLSynchronizeElement synchronize : entityClazz.getSynchronize() ) { synchronizedTableNames.add( synchronize.getTable() ); } @@ -295,7 +295,7 @@ public class HbmEntityBindingState implements EntityBindingState { } @Override - public List getSynchronizedTableNames() { + public Set getSynchronizedTableNames() { return synchronizedTableNames; } } diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/InheritanceTypeTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/InheritanceBindingTest.java similarity index 97% rename from hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/InheritanceTypeTest.java rename to hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/InheritanceBindingTest.java index 9fc6b785ea..39ce58febe 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/InheritanceTypeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/InheritanceBindingTest.java @@ -40,7 +40,7 @@ import static junit.framework.Assert.assertTrue; /** * @author Hardy Ferentschik */ -public class InheritanceTypeTest extends BaseAnnotationBindingTestCase { +public class InheritanceBindingTest extends BaseAnnotationBindingTestCase { @Test public void testNoInheritance() { buildMetadataSources( SingleEntity.class ); diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/SynchronizeBindingTests.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/SynchronizeBindingTests.java new file mode 100644 index 0000000000..bdd6199162 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/SynchronizeBindingTests.java @@ -0,0 +1,75 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, 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.entity; + +import java.util.Set; +import javax.persistence.Entity; +import javax.persistence.Id; + +import org.junit.Test; + +import org.hibernate.annotations.Synchronize; +import org.hibernate.metamodel.binding.EntityBinding; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; + +/** + * Tests for {@code o.h.a.Synchronize}. + * + * @author Hardy Ferentschik + */ +public class SynchronizeBindingTests extends BaseAnnotationBindingTestCase { + @Test + public void testSynchronizeAnnotation() { + buildMetadataSources( TestEntityWithSynchronizeAnnotation.class ); + EntityBinding binding = getEntityBinding( TestEntityWithSynchronizeAnnotation.class ); + Set synchronizedTableNames = binding.getSynchronizedTableNames(); + assertEquals( "Wrong number of synced tables", 2, synchronizedTableNames.size() ); + assertTrue( "Table name missing", synchronizedTableNames.contains( "Foo" ) ); + assertTrue( "Table name missing", synchronizedTableNames.contains( "Bar" ) ); + } + + @Test + public void testNoSynchronizeAnnotation() { + buildMetadataSources( TestEntity.class ); + EntityBinding binding = getEntityBinding( TestEntity.class ); + assertTrue( "There should be no cache binding", binding.getSynchronizedTableNames().size() == 0 ); + } + + @Entity + class TestEntity { + @Id + private int id; + } + + @Entity + @Synchronize( { "Foo", "Bar" }) + class TestEntityWithSynchronizeAnnotation { + @Id + private int id; + } +} + +