Example #1
0
  /**
   * Pega o objeto real que esta sendo utilizado. Isto é necessário pois o hibernate empacota alguns
   * objetos em proxies.
   *
   * @param example o exemplo do bean que será utilizado na conversão
   * @return o objeto real que esta sendo utilizado
   */
  public static Object getBean(Object example) {
    if (example instanceof HibernateProxy) {
      HibernateProxy proxy = (HibernateProxy) example;
      LazyInitializer initializer = proxy.getHibernateLazyInitializer();

      // este código é soh para garantir que não vai dar um lazy initialization.
      SessionImplementor implementor = initializer.getSession();
      if (initializer.isUninitialized()) {
        try {
          // getImplementation is going to want to talk to a session
          if (implementor.isClosed()) {
            // Give up and return example.getClass();
            return example;
          }
        } catch (NoSuchMethodError ex) {
          // We must be using Hibernate 3.0/3.1 which doesn't have
          // this method
        }
      }

      return initializer.getImplementation();
    } else {
      return example;
    }
  }
 /** Associate a proxy that was instantiated by another session with this session */
 private void reassociateProxy(LazyInitializer li, HibernateProxy proxy)
     throws HibernateException {
   if (li.getSession() != this) {
     EntityPersister persister = session.getFactory().getEntityPersister(li.getEntityName());
     EntityKey key = new EntityKey(li.getIdentifier(), persister, session.getEntityMode());
     if (!proxiesByKey.containsKey(key))
       proxiesByKey.put(key, proxy); // any earlier proxy takes precedence
     proxy.getHibernateLazyInitializer().setSession(session);
   }
 }