code: static EntityManager createProxy(final HibernateEntityManagerFactory factory) {
          final EntityManagerInterceptor handler = new EntityManagerInterceptor(factory);
  
          final Enhancer e = new Enhancer();
  
          // make sure we're Serializable and have a write replace method
          e.setInterfaces(new Class[] { EntityManager.class, Serializable.class, IWriteReplace.class });
          e.setSuperclass(Object.class);
          e.setCallback(handler);
          e.setNamingPolicy(new DefaultNamingPolicy() {
              @Override
              public String getClassName(final String prefix,
                                         final String source,
                                         final Object key,
                                         final Predicate names) {
                  return super.getClassName("CROQUET_ENTITY_MANAGER_PROXY_" + prefix, source, key, names);
              }
          });
  
          LOG.trace("Created proxy for an EntityManager");
  
          return (EntityManager)e.create();
      }
docs: Creates a proxy object that will write-replace with a wrapper around the {@link EntityManager}.
  @param factory a factory to generate EntityManagers.
  @return the proxied instance.