HHH-6300 Adding support for @Synchronize annotation

This commit is contained in:
Hardy Ferentschik 2011-06-13 16:06:33 +02:00
parent 5b4e976972
commit 002779dffb
7 changed files with 113 additions and 21 deletions

View File

@ -23,10 +23,8 @@
*/ */
package org.hibernate.metamodel.binding; package org.hibernate.metamodel.binding;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -86,7 +84,7 @@ public class EntityBinding {
private CustomSQL customUpdate; private CustomSQL customUpdate;
private CustomSQL customDelete; private CustomSQL customDelete;
private List<String> synchronizedTableNames; private Set<String> synchronizedTableNames = new HashSet<String>();
public EntityBinding initialize(EntityBindingState state) { public EntityBinding initialize(EntityBindingState state) {
this.isRoot = state.isRoot(); this.isRoot = state.isRoot();
@ -318,13 +316,13 @@ public class EntityBinding {
return isAbstract; return isAbstract;
} }
protected void addSynchronizedTable(String tablename) { protected void addSynchronizedTable(String tableName) {
if ( synchronizedTableNames == null ) { synchronizedTableNames.add( tableName );
synchronizedTableNames = new ArrayList<String>();
}
synchronizedTableNames.add( tablename );
} }
public Set<String> getSynchronizedTableNames() {
return synchronizedTableNames;
}
// Custom SQL ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Custom SQL ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -23,7 +23,7 @@
*/ */
package org.hibernate.metamodel.binding.state; 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.Caching;
import org.hibernate.metamodel.binding.CustomSQL; import org.hibernate.metamodel.binding.CustomSQL;
@ -74,5 +74,5 @@ public interface EntityBindingState {
CustomSQL getCustomDelete(); CustomSQL getCustomDelete();
List<String> getSynchronizedTableNames(); Set<String> getSynchronizedTableNames();
} }

View File

@ -100,6 +100,7 @@ public class EntityBinder {
bindJpaCaching( entityBindingState ); bindJpaCaching( entityBindingState );
bindHibernateCaching( entityBindingState ); bindHibernateCaching( entityBindingState );
bindProxy( entityBindingState ); bindProxy( entityBindingState );
bindSynchronize( entityBindingState );
// take care of the id, attributes and relations // take care of the id, attributes and relations
if ( configuredClass.isRoot() ) { if ( configuredClass.isRoot() ) {
@ -259,6 +260,19 @@ public class EntityBinder {
entityBindingState.setProxyInterfaceName( proxyInterfaceClass ); 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) { private Caching createCachingForCacheableAnnotation(EntityBindingStateImpl entityBindingState) {
String region = entityBindingState.getEntityName(); String region = entityBindingState.getEntityName();
RegionFactory regionFactory = meta.getServiceRegistry().getService( RegionFactory.class ); RegionFactory regionFactory = meta.getServiceRegistry().getService( RegionFactory.class );

View File

@ -23,7 +23,8 @@
*/ */
package org.hibernate.metamodel.source.annotations.entity.state.binding; 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.annotations.OptimisticLockType;
import org.hibernate.metamodel.binding.Caching; import org.hibernate.metamodel.binding.Caching;
@ -67,11 +68,12 @@ public class EntityBindingStateImpl implements EntityBindingState {
private CustomSQL customUpdate; private CustomSQL customUpdate;
private CustomSQL customDelete; private CustomSQL customDelete;
private List<String> synchronizedTableNames; private Set<String> synchronizedTableNames;
public EntityBindingStateImpl(ConfiguredClass configuredClass) { public EntityBindingStateImpl(ConfiguredClass configuredClass) {
this.isRoot = configuredClass.isRoot(); this.isRoot = configuredClass.isRoot();
this.inheritanceType = configuredClass.getInheritanceType(); this.inheritanceType = configuredClass.getInheritanceType();
this.synchronizedTableNames = new HashSet<String>();
// TODO: where do these values come from? // TODO: where do these values come from?
this.metaAttributeContext = null; this.metaAttributeContext = null;
@ -81,7 +83,6 @@ public class EntityBindingStateImpl implements EntityBindingState {
this.customInsert = null; this.customInsert = null;
this.customUpdate = null; this.customUpdate = null;
this.customDelete = null; this.customDelete = null;
this.synchronizedTableNames = null;
} }
public void setEntityName(String entityName) { public void setEntityName(String entityName) {
@ -136,6 +137,10 @@ public class EntityBindingStateImpl implements EntityBindingState {
this.proxyInterfaceName = proxyInterfaceName; this.proxyInterfaceName = proxyInterfaceName;
} }
public void addSynchronizedTableName(String tableName) {
synchronizedTableNames.add( tableName );
}
@Override @Override
public boolean isRoot() { public boolean isRoot() {
return isRoot; return isRoot;
@ -238,7 +243,7 @@ public class EntityBindingStateImpl implements EntityBindingState {
} }
@Override @Override
public List<String> getSynchronizedTableNames() { public Set<String> getSynchronizedTableNames() {
return synchronizedTableNames; return synchronizedTableNames;
} }
} }

View File

@ -23,8 +23,8 @@
*/ */
package org.hibernate.metamodel.source.hbm.state.binding; package org.hibernate.metamodel.source.hbm.state.binding;
import java.util.ArrayList; import java.util.HashSet;
import java.util.List; import java.util.Set;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.cache.spi.access.AccessType; import org.hibernate.cache.spi.access.AccessType;
@ -74,7 +74,7 @@ public class HbmEntityBindingState implements EntityBindingState {
private final CustomSQL customUpdate; private final CustomSQL customUpdate;
private final CustomSQL customDelete; private final CustomSQL customDelete;
private final List<String> synchronizedTableNames; private final Set<String> synchronizedTableNames;
public HbmEntityBindingState( public HbmEntityBindingState(
boolean isRoot, boolean isRoot,
@ -150,7 +150,7 @@ public class HbmEntityBindingState implements EntityBindingState {
} }
if ( entityClazz.getSynchronize() != null ) { if ( entityClazz.getSynchronize() != null ) {
synchronizedTableNames = new ArrayList<String>( entityClazz.getSynchronize().size() ); synchronizedTableNames = new HashSet<String>( entityClazz.getSynchronize().size() );
for ( XMLSynchronizeElement synchronize : entityClazz.getSynchronize() ) { for ( XMLSynchronizeElement synchronize : entityClazz.getSynchronize() ) {
synchronizedTableNames.add( synchronize.getTable() ); synchronizedTableNames.add( synchronize.getTable() );
} }
@ -295,7 +295,7 @@ public class HbmEntityBindingState implements EntityBindingState {
} }
@Override @Override
public List<String> getSynchronizedTableNames() { public Set<String> getSynchronizedTableNames() {
return synchronizedTableNames; return synchronizedTableNames;
} }
} }

View File

@ -40,7 +40,7 @@ import static junit.framework.Assert.assertTrue;
/** /**
* @author Hardy Ferentschik * @author Hardy Ferentschik
*/ */
public class InheritanceTypeTest extends BaseAnnotationBindingTestCase { public class InheritanceBindingTest extends BaseAnnotationBindingTestCase {
@Test @Test
public void testNoInheritance() { public void testNoInheritance() {
buildMetadataSources( SingleEntity.class ); buildMetadataSources( SingleEntity.class );

View File

@ -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<String> 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;
}
}