HHH-5920: improve the performance of PersistentClass

This commit is contained in:
Anton Marsden 2013-07-28 12:56:22 +12:00 committed by Brett Meyer
parent 7bcf161d36
commit ad5c0f1f82
2 changed files with 68 additions and 7 deletions

View File

@ -33,7 +33,6 @@ import java.util.StringTokenizer;
import org.hibernate.EntityMode;
import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.lock.OptimisticLockingStrategy;
import org.hibernate.engine.OptimisticLockStyle;
import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle;
import org.hibernate.engine.spi.Mapping;
@ -60,7 +59,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;
@ -112,6 +114,7 @@ public abstract class PersistentClass implements Serializable, Filterable, MetaA
public void setClassName(String className) {
this.className = className==null ? null : className.intern();
this.mappedClass = null;
}
public String getProxyInterfaceName() {
@ -120,12 +123,16 @@ public abstract class PersistentClass implements Serializable, Filterable, MetaA
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);
@ -135,7 +142,10 @@ public abstract class PersistentClass implements Serializable, Filterable, MetaA
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);
@ -436,10 +446,13 @@ public abstract class PersistentClass implements Serializable, Filterable, MetaA
}
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() + "]" );

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