コード例 #1
0
  @Test
  public void testCascadeAndFetchEntity() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Troop disney = new Troop();
    disney.setName("Disney");
    Soldier mickey = new Soldier();
    mickey.setName("Mickey");
    disney.addSoldier(mickey);
    em.persist(disney);
    em.getTransaction().commit();
    em.close();

    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Soldier soldier = em.find(Soldier.class, mickey.getId());
    assertFalse(Hibernate.isInitialized(soldier.getTroop()));
    em.getTransaction().commit();
    assertFalse(Hibernate.isInitialized(soldier.getTroop()));
    em.close();
    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Troop troop = em.find(Troop.class, disney.getId());
    em.remove(troop);
    // Fail because of HHH-1187
    em.getTransaction().commit();
    em.close();
  }
コード例 #2
0
  @Test
  public void testPerfCascadeAndFetchEntity() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    // init data
    em.getTransaction().begin();
    int loop = 50;
    for (int i = 0; i < loop; i++) {
      Troop disney = new Troop();
      disney.setName("Disney");
      Soldier mickey = new Soldier();
      mickey.setName("Mickey");
      disney.addSoldier(mickey);
      em.persist(disney);
    }
    em.getTransaction().commit();
    em.close();

    // Warm up loop
    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    for (int i = 0; i < loop; i++) {
      // Soldier soldier = em.find( Soldier.class, new Integer(i) );
      Troop troop = em.find(Troop.class, new Integer(i));
      // ( ( HibernateEntityManager ) em ).getSession().evict(soldier);
    }
    long l11 = System.currentTimeMillis();
    Query query = em.createQuery("SELECT count(t) FROM Soldier t");
    query.getSingleResult();
    long l2 = System.currentTimeMillis();
    System.out.println("Query1 " + (l2 - l11));
    em.getTransaction().commit();
    em.close();

    // do not evict
    for (int j = 0; j < 10; j++) {
      em = getOrCreateEntityManager();
      em.getTransaction().begin();
      for (int i = 0; i < loop; i++) {
        Troop troop = em.find(Troop.class, new Integer(i));
        ((HibernateEntityManager) em).getSession().evict(troop);
      }
      l11 = System.currentTimeMillis();
      query = em.createQuery("SELECT count(t) FROM Soldier t");
      query.getSingleResult();
      l2 = System.currentTimeMillis();
      System.out.println("Query " + (l2 - l11));
      em.getTransaction().commit();
      em.close();

      // evict
      em = getOrCreateEntityManager();
      em.getTransaction().begin();
      for (int i = 0; i < loop; i++) {
        // Soldier soldier = em.find( Soldier.class, new Integer(i) );
        Troop troop = em.find(Troop.class, new Integer(i));

        // ( ( HibernateEntityManager ) em ).getSession().evict(troop);
      }
      l11 = System.currentTimeMillis();
      query = em.createQuery("SELECT count(t) FROM Soldier t");
      query.getSingleResult();
      l2 = System.currentTimeMillis();
      System.out.println("Query " + (l2 - l11));
      em.getTransaction().commit();
    }
    em.close();
  }