@Test
  public void testWrappedFromEntityManagerAPI() throws Exception {
    buildArchive();

    final EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpajtastandalone");
    assertThat(HibernateEntityManagerFactory.class.isAssignableFrom(emf.getClass())).isTrue();
    SessionFactory factory = ((HibernateEntityManagerFactory) emf).getSessionFactory();
    assertThat(factory.getClass()).isEqualTo(OgmSessionFactory.class);

    Session s = factory.openSession();
    assertThat(s.getClass()).isEqualTo(OgmSession.class);
    assertThat(s.getSessionFactory().getClass()).isEqualTo(OgmSessionFactory.class);

    Cavalier cavalier = new Cavalier();
    cavalier.setName("Caroline");

    EntityManager em = emf.createEntityManager();
    assertThat(em.unwrap(Session.class).getClass()).isEqualTo(OgmSession.class);
    assertThat(em.getDelegate().getClass()).isEqualTo(OgmSession.class);

    s.save(cavalier);

    Cavalier c = (Cavalier) s.get(Cavalier.class, cavalier.getId());
    assertTrue(c.getId().equals(cavalier.getId()));
    assertTrue(c.getName().equals(cavalier.getName()));

    em.close();

    emf.close();
  }
    @Override
    public ManagedReference getReference() {
      PersistenceUnitService service =
          (PersistenceUnitService)
              deploymentUnit.getServiceRegistry().getRequiredService(puServiceName).getValue();
      EntityManagerFactory emf = service.getEntityManagerFactory();

      if (!ENTITY_MANAGER_FACTORY_CLASS.equals(
          injectionTypeName)) { // inject non-standard wrapped class (e.g.
                                // org.hibernate.SessionFactory)
        Class extensionClass;
        try {
          // make sure we can access the target class type
          extensionClass = pu.getClassLoader().loadClass(injectionTypeName);
        } catch (ClassNotFoundException e) {
          throw new RuntimeException(
              "couldn't load " + injectionTypeName + " from JPA modules classloader", e);
        }
        // TODO:  when/if jpa supports unwrap, change to
        //   Object targetValueToInject = emf.unwrap(extensionClass);
        // Until jpa supports unwrap on sessionfactory, only support hibernate

        Method getSessionFactory;
        try {
          getSessionFactory = emf.getClass().getMethod("getSessionFactory");
        } catch (NoSuchMethodException e) {
          throw new RuntimeException(
              "Can only inject from a Hibernate EntityManagerFactoryImpl", e);
        }

        Object targetValueToInject = null;
        try {
          targetValueToInject = getSessionFactory.invoke(emf, new Object[0]);
        } catch (IllegalAccessException e) {
          throw new RuntimeException(
              "Couldn't get Hibernate session factory from entity manager", e);
        } catch (InvocationTargetException e) {
          throw new RuntimeException(
              "Couldn't get Hibernate session factory from entity manager", e);
        }
        return new ValueManagedReference(new ImmediateValue<Object>(targetValueToInject));
      }

      return new ValueManagedReference(new ImmediateValue<Object>(emf));
    }
  @Test
  public void testWrappedFromEntityManagerAPI() throws Exception {
    final EntityManagerFactory emf =
        Persistence.createEntityManagerFactory("ogm", TestHelper.getEnvironmentProperties());
    assertThat(HibernateEntityManagerFactory.class.isAssignableFrom(emf.getClass())).isTrue();
    SessionFactory factory = ((HibernateEntityManagerFactory) emf).getSessionFactory();
    assertThat(factory.getClass()).isEqualTo(OgmSessionFactoryImpl.class);

    Session s = factory.openSession();
    assertThat(s.getClass()).isEqualTo(OgmSessionImpl.class);
    assertThat(s.getSessionFactory().getClass()).isEqualTo(OgmSessionFactoryImpl.class);
    s.close();

    EntityManager em = emf.createEntityManager();
    assertThat(em.unwrap(Session.class) instanceof OgmSession);
    assertThat(em.getDelegate().getClass()).isEqualTo(OgmSessionImpl.class);

    em.close();

    emf.close();
  }