HHH-6263 Binding @Proxy. Adding some tests

This commit is contained in:
Hardy Ferentschik 2011-05-26 18:21:53 +02:00
parent bd900d9bb1
commit 227141e643
8 changed files with 222 additions and 51 deletions

View File

@ -101,6 +101,7 @@ public class EntityBinding {
// go ahead and set the lazy here, since pojo.proxy can override it. // go ahead and set the lazy here, since pojo.proxy can override it.
lazy = MappingHelper.getBooleanValue( entityClazz.isLazy(), defaults.isDefaultLazy() ); lazy = MappingHelper.getBooleanValue( entityClazz.isLazy(), defaults.isDefaultLazy() );
proxyInterfaceName = entityClazz.getProxy();
discriminatorValue = MappingHelper.getStringValue( entityClazz.getDiscriminatorValue(), entity.getName() ); discriminatorValue = MappingHelper.getStringValue( entityClazz.getDiscriminatorValue(), entity.getName() );
dynamicUpdate = entityClazz.isDynamicUpdate(); dynamicUpdate = entityClazz.isDynamicUpdate();
dynamicInsert = entityClazz.isDynamicInsert(); dynamicInsert = entityClazz.isDynamicInsert();
@ -319,6 +320,14 @@ public class EntityBinding {
this.lazy = lazy; this.lazy = lazy;
} }
public void setProxyInterfaceName(String proxyInterfaceName) {
this.proxyInterfaceName = proxyInterfaceName;
}
public String getProxyInterfaceName() {
return proxyInterfaceName;
}
public String getWhereFilter() { public String getWhereFilter() {
return whereFilter; return whereFilter;
} }

View File

@ -89,6 +89,7 @@ public class EntityBinder {
bindWhereFilter( entityBinding ); bindWhereFilter( entityBinding );
bindJpaCaching( entityBinding ); bindJpaCaching( entityBinding );
bindHibernateCaching( entityBinding ); bindHibernateCaching( entityBinding );
bindProxy( entityBinding );
// take care of the id, attributes and relations // take care of the id, attributes and relations
if ( configuredClass.isRoot() ) { if ( configuredClass.isRoot() ) {
@ -228,6 +229,29 @@ public class EntityBinder {
} }
} }
private void bindProxy(EntityBinding entityBinding) {
AnnotationInstance proxyAnnotation = JandexHelper.getSingleAnnotation(
configuredClass.getClassInfo(), HibernateDotNames.PROXY
);
boolean lazy = true;
String proxyInterfaceClass = null;
if ( proxyAnnotation != null ) {
AnnotationValue lazyValue = proxyAnnotation.value( "lazy" );
if ( lazyValue != null ) {
lazy = lazyValue.asBoolean();
}
AnnotationValue proxyClassValue = proxyAnnotation.value( "proxyClass" );
if ( proxyClassValue != null ) {
proxyInterfaceClass = proxyClassValue.asString();
}
}
entityBinding.setLazy( lazy );
entityBinding.setProxyInterfaceName( proxyInterfaceClass );
}
private Caching createCachingForCacheableAnnotation(EntityBinding entityBinding) { private Caching createCachingForCacheableAnnotation(EntityBinding entityBinding) {
String region = entityBinding.getEntity().getName(); String region = entityBinding.getEntity().getName();
RegionFactory regionFactory = meta.getServiceRegistry().getService( RegionFactory.class ); RegionFactory regionFactory = meta.getServiceRegistry().getService( RegionFactory.class );

View File

@ -151,7 +151,7 @@ abstract class AbstractEntityBinder {
private void bindPojoRepresentation(XMLHibernateMapping.XMLClass entityClazz, private void bindPojoRepresentation(XMLHibernateMapping.XMLClass entityClazz,
EntityBinding entityBinding) { EntityBinding entityBinding) {
String className = hibernateMappingBinder.getClassName( entityClazz.getName() ); String className = hibernateMappingBinder.getClassName( entityClazz.getName() );
String proxyName = hibernateMappingBinder.getClassName( entityClazz.getProxy() ); String proxyName = entityBinding.getProxyInterfaceName();
entityBinding.getEntity().getPojoEntitySpecifics().setClassName( className ); entityBinding.getEntity().getPojoEntitySpecifics().setClassName( className );

View File

@ -28,8 +28,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.dom4j.Attribute;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.cfg.NamingStrategy; import org.hibernate.cfg.NamingStrategy;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
@ -97,11 +95,6 @@ public class HbmBinder implements MappingDefaults {
return metadata; return metadata;
} }
XMLHibernateMapping getHibernateMapping() {
return hibernateMapping;
}
Origin getOrigin() { Origin getOrigin() {
return jaxbRoot.getOrigin(); return jaxbRoot.getOrigin();
} }
@ -252,12 +245,7 @@ public class HbmBinder implements MappingDefaults {
return HbmHelper.extractEntityName( entityClazz, packageName ); return HbmHelper.extractEntityName( entityClazz, packageName );
} }
String getClassName(Attribute attribute) {
return HbmHelper.getClassName( attribute, packageName );
}
String getClassName(String unqualifiedName) { String getClassName(String unqualifiedName) {
return HbmHelper.getClassName( unqualifiedName, packageName ); return HbmHelper.getClassName( unqualifiedName, packageName );
} }
} }

View File

@ -0,0 +1,62 @@
/*
* 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.entity;
import org.junit.After;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.testing.junit4.BaseUnitTestCase;
/**
* @author Hardy Ferentschik
*/
public abstract class BaseAnnotationBindingTestCase extends BaseUnitTestCase {
protected MetadataSources sources;
protected MetadataImpl meta;
@After
public void tearDown() {
sources = null;
meta = null;
}
public void buildMetadataSources(Class<?>... classes) {
sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() );
for ( Class clazz : classes ) {
sources.addAnnotatedClass( clazz );
}
}
public EntityBinding getEntityBinding(Class<?> clazz) {
if ( meta == null ) {
meta = (MetadataImpl) sources.buildMetadata();
}
return meta.getEntityBinding( clazz.getName() );
}
}

View File

@ -33,12 +33,8 @@ import org.junit.Test;
import org.hibernate.annotations.Cache; import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.cache.spi.access.AccessType; import org.hibernate.cache.spi.access.AccessType;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.binding.Caching; import org.hibernate.metamodel.binding.Caching;
import org.hibernate.metamodel.binding.EntityBinding; import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertNotNull;
@ -49,10 +45,12 @@ import static junit.framework.Assert.assertNull;
* *
* @author Hardy Ferentschik * @author Hardy Ferentschik
*/ */
public class CacheBindingTests extends BaseUnitTestCase { public class CacheBindingTests extends BaseAnnotationBindingTestCase {
@Test @Test
public void testHibernateCaching() { public void testHibernateCaching() {
EntityBinding binding = getEntityBinding( HibernateCacheEntity.class, SharedCacheMode.ALL ); buildMetadataSources( HibernateCacheEntity.class );
sources.getMetadataBuilder().with( SharedCacheMode.ALL );
EntityBinding binding = getEntityBinding( HibernateCacheEntity.class );
assertNotNull( "There should be a cache binding", binding.getCaching() ); assertNotNull( "There should be a cache binding", binding.getCaching() );
Caching caching = binding.getCaching(); Caching caching = binding.getCaching();
assertEquals( "Wrong region", "foo", caching.getRegion() ); assertEquals( "Wrong region", "foo", caching.getRegion() );
@ -62,7 +60,9 @@ public class CacheBindingTests extends BaseUnitTestCase {
@Test @Test
public void testJpaCaching() { public void testJpaCaching() {
EntityBinding binding = getEntityBinding( JpaCacheEntity.class, SharedCacheMode.ALL ); buildMetadataSources( JpaCacheEntity.class );
sources.getMetadataBuilder().with( SharedCacheMode.ALL );
EntityBinding binding = getEntityBinding( JpaCacheEntity.class );
assertNotNull( "There should be a cache binding", binding.getCaching() ); assertNotNull( "There should be a cache binding", binding.getCaching() );
Caching caching = binding.getCaching(); Caching caching = binding.getCaching();
assertEquals( assertEquals(
@ -75,19 +75,12 @@ public class CacheBindingTests extends BaseUnitTestCase {
@Test @Test
public void testNoCaching() { public void testNoCaching() {
EntityBinding binding = getEntityBinding( NoCacheEntity.class, SharedCacheMode.NONE ); buildMetadataSources( NoCacheEntity.class );
sources.getMetadataBuilder().with( SharedCacheMode.NONE );
EntityBinding binding = getEntityBinding( NoCacheEntity.class );
assertNull( "There should be no cache binding", binding.getCaching() ); assertNull( "There should be no cache binding", binding.getCaching() );
} }
private EntityBinding getEntityBinding(Class<?> clazz, SharedCacheMode cacheMode) {
MetadataSources sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() );
sources.addAnnotatedClass( clazz );
sources.getMetadataBuilder().with( cacheMode );
MetadataImpl metadata = (MetadataImpl) sources.buildMetadata();
return metadata.getEntityBinding( this.getClass().getName() + "$" + clazz.getSimpleName() );
}
@Entity @Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "foo", include = "non-lazy") @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "foo", include = "non-lazy")
class HibernateCacheEntity { class HibernateCacheEntity {

View File

@ -29,11 +29,7 @@ import javax.persistence.Id;
import org.junit.Test; import org.junit.Test;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.binding.EntityBinding; import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertNull;
@ -41,33 +37,23 @@ import static junit.framework.Assert.assertNull;
/** /**
* @author Hardy Ferentschik * @author Hardy Ferentschik
*/ */
public class InheritanceTypeTest extends BaseUnitTestCase { public class InheritanceTypeTest extends BaseAnnotationBindingTestCase {
@Test @Test
public void testNoInheritance() { public void testNoInheritance() {
MetadataImpl meta = buildMetadata( SingleEntity.class ); buildMetadataSources( SingleEntity.class );
EntityBinding entityBinding = getEntityBindingForInnerClass( meta, SingleEntity.class ); EntityBinding entityBinding = getEntityBinding( SingleEntity.class );
assertNull( entityBinding.getEntityDiscriminator() ); assertNull( entityBinding.getEntityDiscriminator() );
} }
@Test @Test
public void testDiscriminatorValue() { public void testDiscriminatorValue() {
MetadataImpl meta = buildMetadata( RootOfSingleTableInheritance.class, SubclassOfSingleTableInheritance.class ); buildMetadataSources(
EntityBinding entityBinding = meta.getEntityBinding( SubclassOfSingleTableInheritance.class.getName() ); RootOfSingleTableInheritance.class, SubclassOfSingleTableInheritance.class
);
EntityBinding entityBinding = getEntityBinding( SubclassOfSingleTableInheritance.class );
assertEquals( "Wrong discriminator value", "foo", entityBinding.getDiscriminatorValue() ); assertEquals( "Wrong discriminator value", "foo", entityBinding.getDiscriminatorValue() );
} }
private MetadataImpl buildMetadata(Class<?>... classes) {
MetadataSources sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() );
for ( Class clazz : classes ) {
sources.addAnnotatedClass( clazz );
}
return (MetadataImpl) sources.buildMetadata();
}
private EntityBinding getEntityBindingForInnerClass(MetadataImpl meta, Class<?> clazz) {
return meta.getEntityBinding( this.getClass().getName() + "$" + clazz.getSimpleName() );
}
@Entity @Entity
class SingleEntity { class SingleEntity {
@Id @Id

View File

@ -0,0 +1,109 @@
/*
* 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.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.junit.Test;
import org.hibernate.annotations.Proxy;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
/**
* Tests for {@code o.h.a.Cache}.
*
* @author Hardy Ferentschik
*/
public class ProxyBindingTests extends BaseAnnotationBindingTestCase {
@Test
public void testProxyNoAttributes() {
buildMetadataSources( ProxiedEntity.class );
EntityBinding binding = getEntityBinding( ProxiedEntity.class );
assertTrue( "Wrong laziness", binding.isLazy() );
assertEquals( "Wrong proxy interface", null, binding.getProxyInterfaceName() );
}
@Test
public void testNoProxy() {
buildMetadataSources(NoProxyEntity.class);
EntityBinding binding = getEntityBinding( NoProxyEntity.class );
assertTrue( "Wrong laziness", binding.isLazy() );
assertEquals( "Wrong proxy interface", null, binding.getProxyInterfaceName() );
}
@Test
public void testProxyDisabled() {
buildMetadataSources( ProxyDisabledEntity.class );
EntityBinding binding = getEntityBinding( ProxyDisabledEntity.class );
assertFalse( "Wrong laziness", binding.isLazy() );
assertEquals( "Wrong proxy interface", null, binding.getProxyInterfaceName() );
}
@Test
public void testProxyInterface() {
buildMetadataSources( ProxyInterfaceEntity.class );
EntityBinding binding = getEntityBinding( ProxyInterfaceEntity.class );
assertTrue( "Wrong laziness", binding.isLazy() );
assertEquals(
"Wrong proxy interface",
"org.hibernate.metamodel.source.annotations.entity.ProxyBindingTests$ProxyInterfaceEntity",
binding.getProxyInterfaceName()
);
}
@Entity
class NoProxyEntity {
@Id
private int id;
}
@Entity
@Proxy
class ProxiedEntity {
@Id
private int id;
}
@Entity
@Proxy(lazy = false)
class ProxyDisabledEntity {
@Id
private int id;
}
@Entity
@Proxy(proxyClass = ProxyInterfaceEntity.class)
class ProxyInterfaceEntity {
@Id
private int id;
}
}