HHH-3718 - Do not initialize proxy upon call to ID getter when using

AccessType.FIELD
This commit is contained in:
Michael Rudolf 2012-01-26 12:17:01 +01:00 committed by Strong Liu
parent 8f223d199b
commit 22a6283e6d
4 changed files with 195 additions and 2 deletions

View File

@ -25,6 +25,7 @@ package org.hibernate.property;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.Locale;
import java.util.Map;
import org.hibernate.HibernateException;
@ -44,6 +45,9 @@ public class DirectPropertyAccessor implements PropertyAccessor {
private final transient Field field;
private final Class clazz;
private final String name;
private transient Method method;
private transient boolean methodLookedUp = false;
DirectGetter(Field field, Class clazz, String name) {
this.field = field;
this.clazz = clazz;
@ -80,14 +84,25 @@ public class DirectPropertyAccessor implements PropertyAccessor {
* {@inheritDoc}
*/
public Method getMethod() {
return null;
if ( !methodLookedUp ) {
//HHH-3718: Return method for identifier access to prevent initialization
try {
String readMethodName = "get" +
name.substring(0, 1).toUpperCase( Locale.ENGLISH ) +
name.substring(1);
method = clazz.getMethod(readMethodName);
} catch (Exception ex) { /* ignore */ }
methodLookedUp = true;
}
return method;
}
/**
* {@inheritDoc}
*/
public String getMethodName() {
return null;
Method method = getMethod();
return method == null ? null : method.getName();
}
/**

View File

@ -0,0 +1,71 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Middleware LLC 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 Middleware LLC.
*
* 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.property;
import java.util.Date;
import org.junit.Test;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertFalse;
/**
* @author Michael Rudolf
*/
public class DirectPropertyAccessorTest extends BaseCoreFunctionalTestCase {
@Test
@TestForIssue( jiraKey="HHH-3718" )
public void testDirectIdPropertyAccess() throws Exception {
Session s = openSession();
final Transaction transaction = s.beginTransaction();
Item i = new Item();
s.persist( i );
Order o = new Order();
o.setOrderNumber( 1 );
o.getItems().add( i );
s.persist( o );
transaction.commit();
s.clear();
o = ( Order ) s.load( Order.class, 1 );
assertFalse( Hibernate.isInitialized( o ) );
o.getOrderNumber();
assertFalse( Hibernate.isInitialized( o ) );
s.close();
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Order.class,
Item.class,
};
}
}

View File

@ -0,0 +1,47 @@
// $Id$
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Middleware LLC 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 Middleware LLC.
*
* 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.property;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* @author Michael Rudolf
*/
@Entity
public class Item implements Serializable {
@Id
private int itemNumber;
public int getItemNumber() {
return itemNumber;
}
public void setItemNumber(int itemNumber) {
this.itemNumber = itemNumber;
}
}

View File

@ -0,0 +1,60 @@
// $Id$
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Middleware LLC 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 Middleware LLC.
*
* 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.property;
import java.io.Serializable;
import java.util.Set;
import java.util.HashSet;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* @author Michael Rudolf
*/
@Entity
@Table(name="C_ORDER")
public class Order implements Serializable {
@Id
private int orderNumber;
@OneToMany( fetch = FetchType.LAZY )
private Set<Item> items = new HashSet<Item>();
public int getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(int orderNumber) {
this.orderNumber = orderNumber;
}
public Set<Item> getItems() {
return items;
}
}