HHH-5920: improve the performance of PersistentClass

Conflicts:
	hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java
This commit is contained in:
Anton Marsden 2013-08-19 15:53:56 -04:00 committed by Brett Meyer
parent 131c108e4b
commit 2396e6b99e
2 changed files with 69 additions and 7 deletions

View File

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

View File

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