首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

hibernate 懒加载有关问题的一个临时解决方案

2012-09-14 
hibernate 懒加载问题的一个临时解决方案hibernate的懒加载问题时常会困扰着我们, 今天发现了hibernate自

hibernate 懒加载问题的一个临时解决方案

hibernate的懒加载问题时常会困扰着我们, 今天发现了hibernate自身也提供一些解决办法

Hibernate.initialize(company.getUsers());? 这样就加载了users集合

但不支持递归加载

/** * Force initialization of a proxy or persistent collection. * <p/> * Note: This only ensures intialization of a proxy object or collection; * it is not guaranteed that the elements INSIDE the collection will be initialized/materialized. * * @param proxy a persistable object, proxy, persistent collection or <tt>null</tt> * @throws HibernateException if we can't initialize the proxy at this time, eg. the <tt>Session</tt> was closed */public static void initialize(Object proxy) throws HibernateException {if ( proxy == null ) {return;}else if ( proxy instanceof HibernateProxy ) {( ( HibernateProxy ) proxy ).getHibernateLazyInitializer().initialize();}else if ( proxy instanceof PersistentCollection ) {( ( PersistentCollection ) proxy ).forceInitialization();}}/** * Check if the proxy or persistent collection is initialized. * * @param proxy a persistable object, proxy, persistent collection or <tt>null</tt> * @return true if the argument is already initialized, or is not a proxy or collection */public static boolean isInitialized(Object proxy) {if ( proxy instanceof HibernateProxy ) {return !( ( HibernateProxy ) proxy ).getHibernateLazyInitializer().isUninitialized();}else if ( proxy instanceof PersistentCollection ) {return ( ( PersistentCollection ) proxy ).wasInitialized();}else {return true;}}

热点排行