From 2396e6b99e9a3e8bead07bec8918e48e4a7dd505 Mon Sep 17 00:00:00 2001 From: Anton Marsden Date: Mon, 19 Aug 2013 15:53:56 -0400 Subject: [PATCH] HHH-5920: improve the performance of PersistentClass Conflicts: hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java --- .../hibernate/mapping/PersistentClass.java | 28 ++++++++--- .../test/mapping/PersistentClassTest.java | 48 +++++++++++++++++++ 2 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/mapping/PersistentClassTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java b/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java index e8a299dd48..f48e282a3b 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java @@ -58,7 +58,10 @@ public abstract class PersistentClass implements Serializable, Filterable, MetaA private String entityName; private String className; + private transient Class mappedClass; + private String proxyInterfaceName; + private transient Class proxyInterface; private String nodeName; private String jpaEntityName; @@ -110,6 +113,7 @@ public String getClassName() { public void setClassName(String className) { this.className = className==null ? null : className.intern(); + this.mappedClass = null; } public String getProxyInterfaceName() { @@ -118,12 +122,16 @@ public String getProxyInterfaceName() { public void setProxyInterfaceName(String proxyInterfaceName) { this.proxyInterfaceName = proxyInterfaceName; + this.proxyInterface = null; } public Class getMappedClass() throws MappingException { if (className==null) return null; try { - return ReflectHelper.classForName(className); + if(mappedClass == null) { + mappedClass = ReflectHelper.classForName(className); + } + return mappedClass; } catch (ClassNotFoundException cnfe) { throw new MappingException("entity class not found: " + className, cnfe); @@ -133,7 +141,10 @@ public Class getMappedClass() throws MappingException { public Class getProxyInterface() { if (proxyInterfaceName==null) return null; try { - return ReflectHelper.classForName( proxyInterfaceName ); + if(proxyInterface == null) { + proxyInterface = ReflectHelper.classForName( proxyInterfaceName ); + } + return proxyInterface; } catch (ClassNotFoundException cnfe) { throw new MappingException("proxy class not found: " + proxyInterfaceName, cnfe); @@ -434,10 +445,13 @@ else if ( identifierProperty == null && getIdentifierMapper() != null ) { } private Property getProperty(String propertyName, Iterator iterator) throws MappingException { - while ( iterator.hasNext() ) { - Property prop = (Property) iterator.next(); - if ( prop.getName().equals( StringHelper.root(propertyName) ) ) { - return prop; + if(iterator.hasNext()) { + String root = StringHelper.root(propertyName); + while ( iterator.hasNext() ) { + Property prop = (Property) iterator.next(); + if ( prop.getName().equals( root ) ) { + return prop; + } } } throw new MappingException( "property [" + propertyName + "] not found on entity [" + getEntityName() + "]" ); @@ -866,4 +880,4 @@ public void setSuperMappedSuperclass(MappedSuperclass superMappedSuperclass) { // End of @Mappedsuperclass support -} \ No newline at end of file +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/mapping/PersistentClassTest.java b/hibernate-core/src/test/java/org/hibernate/test/mapping/PersistentClassTest.java new file mode 100644 index 0000000000..fe6e6b188c --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/mapping/PersistentClassTest.java @@ -0,0 +1,48 @@ +package org.hibernate.test.mapping; + +import org.hibernate.MappingException; +import org.hibernate.mapping.Property; +import org.hibernate.mapping.RootClass; +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.junit.Assert; +import org.junit.Test; + +public class PersistentClassTest extends BaseUnitTestCase { + + @Test + public void testGetMappedClass() { + RootClass pc = new RootClass(); + pc.setClassName(String.class.getName()); + Assert.assertEquals(String.class.getName(), pc.getClassName()); + Assert.assertEquals(String.class, pc.getMappedClass()); + pc.setClassName(Integer.class.getName()); + Assert.assertEquals(Integer.class, pc.getMappedClass()); + } + + @Test + public void testGetProxyInterface() { + RootClass pc = new RootClass(); + pc.setProxyInterfaceName(String.class.getName()); + Assert.assertEquals(String.class.getName(), pc.getProxyInterfaceName()); + Assert.assertEquals(String.class, pc.getProxyInterface()); + pc.setProxyInterfaceName(Integer.class.getName()); + Assert.assertEquals(Integer.class, pc.getProxyInterface()); + } + + @Test + public void testGetProperty() { + RootClass pc = new RootClass(); + Property p = new Property(); + p.setName("name"); + pc.addProperty(p); + Assert.assertEquals(p, pc.getProperty("name")); + Assert.assertEquals(p, pc.getProperty("name.test")); + try { + Assert.assertNull(pc.getProperty("test")); + Assert.fail("MappingException expected"); + } catch (MappingException e) { + // expected + } + } + +}