将Hibernate 2.1.8升级到Hibernate 3.6.10

时间:2014-12-19 11:31:54

标签: java hibernate

我们正在尝试将遗留应用程序从Hibernate 2.1.8升级到Hibernate 3.6.10。该应用程序使用几个使用hibernate 2.1.8 api编写的类来提供自己的持久性机制。问题是Hibernate 3.6.10不再具有那些api的功能,因此我们需要重写类。你能帮助我们在hibernate 3中找到适当的匹配项:

HibernateProxyHelper.getLazyInitializer((HibernateProxy)对象).isUninitialized()

您可以看到以下代码

import java.util.Collection;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.collection.PersistentCollection;
import net.sf.hibernate.proxy.HibernateProxy;
import net.sf.hibernate.proxy.HibernateProxyHelper;
import com.******.component.AbstractComponent;
import com.******.****.persistence.ILazyInitializationProxyStrategy;
import com.******.****.persistence.PersistenceException;

/*
 *  Initialize Hibernate proxies
 */
public class HibernateLazyInitializationProxyStrategy extends AbstractComponent
    implements ILazyInitializationProxyStrategy {

/**
 * @return is the object an uninitialized hibernate proxy  
 */
public boolean isProxy(Object object) {
    return isObjectProxy(object) || isCollectionProxy(object);
}


public boolean isObjectProxy(Object object) {
    return object instanceof HibernateProxy && HibernateProxyHelper.getLazyInitializer((HibernateProxy)object).isUninitialized();
}


public boolean isCollectionProxy(Object object) {
    return object instanceof PersistentCollection && !((PersistentCollection)object).wasInitialized();
}
public Object initializeProxy(Object object) throws PersistenceException{
   Object returnValue = object;
    if (object instanceof HibernateProxy) {
       try {
        if (HibernateProxyHelper.getLazyInitializer((HibernateProxy)object).isUninitialized()) {
          HibernateProxyHelper.getLazyInitializer((HibernateProxy)object).initialize();
        }
        returnValue =  HibernateProxyHelper.getLazyInitializer((HibernateProxy)object).getImplementation();
        } catch (HibernateException e) {
            // TODO Auto-generated catch block
           throw new PersistenceException(e);
        }  
    } else if(isCollectionProxy(object)) { 
        ((Collection)object).size();
    }
    return returnValue; 
}

}

任何帮助将不胜感激:)

1 个答案:

答案 0 :(得分:0)

如果您想使用Hibernate API(非纯JPA),您可以使用方法Hibernate.isInitialized(java.lang.Object)

如果您可以使用JPA2,可以使用PersistenceUnitUtil进行,如下所示

PersistenceUnitUtil unitUtil = em.getEntityManagerFactory().getPersistenceUnitUtil();

Assert.assertTrue(unitUtil.isLoaded(myclassInstance));

Assert.assertFalse(unitUtil.isLoaded(myclassInstance, "myChildObjectList")); //for object collections