Search in sources :

Example 1 with EntityStatistics

use of org.hibernate.stat.EntityStatistics in project hibernate-orm by hibernate.

the class DynamicMapOneToOneTest method testOneToOneOnSubclass.

@Test
public void testOneToOneOnSubclass() {
    Map person = new HashMap();
    person.put("name", "Steve");
    Map address = new HashMap();
    address.put("zip", "12345");
    address.put("state", "TX");
    address.put("street", "123 Main St");
    person.put("address", address);
    address.put("owner", person);
    Session s = openSession();
    s.beginTransaction();
    s.persist("Person", person);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    EntityStatistics addressStats = sessionFactory().getStatistics().getEntityStatistics("Address");
    person = (Map) s.createQuery("from Person p join fetch p.address").uniqueResult();
    assertNotNull("could not locate person", person);
    assertNotNull("could not locate persons address", person.get("address"));
    s.clear();
    Object[] tuple = (Object[]) s.createQuery("select p.name, p from Person p join fetch p.address").uniqueResult();
    assertEquals(tuple.length, 2);
    person = (Map) tuple[1];
    assertNotNull("could not locate person", person);
    assertNotNull("could not locate persons address", person.get("address"));
    s.delete("Person", person);
    s.getTransaction().commit();
    s.close();
    assertEquals(addressStats.getFetchCount(), 0);
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) EntityStatistics(org.hibernate.stat.EntityStatistics) Session(org.hibernate.Session) Test(org.junit.Test)

Example 2 with EntityStatistics

use of org.hibernate.stat.EntityStatistics in project hibernate-orm by hibernate.

the class JoinedSubclassOneToOneTest method testOneToOneOnSubclass.

@Test
public void testOneToOneOnSubclass() {
    Person p = new Person();
    p.name = "Gavin";
    Address a = new Address();
    a.entityName = "Gavin";
    a.zip = "3181";
    a.state = "VIC";
    a.street = "Karbarook Ave";
    p.address = a;
    Session s = openSession();
    Transaction t = s.beginTransaction();
    s.persist(p);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    EntityStatistics addressStats = sessionFactory().getStatistics().getEntityStatistics(Address.class.getName());
    EntityStatistics mailingAddressStats = sessionFactory().getStatistics().getEntityStatistics("MailingAddress");
    p = (Person) s.createQuery("from Person p join fetch p.address left join fetch p.mailingAddress").uniqueResult();
    assertNotNull(p.address);
    assertNull(p.mailingAddress);
    s.clear();
    p = (Person) s.createQuery("select p from Person p join fetch p.address left join fetch p.mailingAddress").uniqueResult();
    assertNotNull(p.address);
    assertNull(p.mailingAddress);
    s.clear();
    Object[] stuff = (Object[]) s.createQuery("select p.name, p from Person p join fetch p.address left join fetch p.mailingAddress").uniqueResult();
    assertEquals(stuff.length, 2);
    p = (Person) stuff[1];
    assertNotNull(p.address);
    assertNull(p.mailingAddress);
    s.clear();
    assertEquals(addressStats.getFetchCount(), 0);
    assertEquals(mailingAddressStats.getFetchCount(), 0);
    p = (Person) s.createQuery("from Person p join fetch p.address").uniqueResult();
    assertNotNull(p.address);
    assertNull(p.mailingAddress);
    s.clear();
    assertEquals(addressStats.getFetchCount(), 0);
    assertEquals(mailingAddressStats.getFetchCount(), 1);
    p = (Person) s.createQuery("from Person").uniqueResult();
    assertNotNull(p.address);
    assertNull(p.mailingAddress);
    s.clear();
    assertEquals(addressStats.getFetchCount(), 0);
    assertEquals(mailingAddressStats.getFetchCount(), 2);
    p = (Person) s.createQuery("from Entity").uniqueResult();
    assertNotNull(p.address);
    assertNull(p.mailingAddress);
    s.clear();
    assertEquals(addressStats.getFetchCount(), 0);
    assertEquals(mailingAddressStats.getFetchCount(), 3);
    //note that in here join fetch is used for the nullable
    //one-to-one, due to a very special case of default
    p = (Person) s.get(Person.class, "Gavin");
    assertNotNull(p.address);
    assertNull(p.mailingAddress);
    s.clear();
    assertEquals(addressStats.getFetchCount(), 0);
    assertEquals(mailingAddressStats.getFetchCount(), 3);
    p = (Person) s.get(Entity.class, "Gavin");
    assertNotNull(p.address);
    assertNull(p.mailingAddress);
    s.clear();
    assertEquals(addressStats.getFetchCount(), 0);
    assertEquals(mailingAddressStats.getFetchCount(), 3);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    Org org = new Org();
    org.name = "IFA";
    Address a2 = new Address();
    a2.entityName = "IFA";
    a2.zip = "3181";
    a2.state = "VIC";
    a2.street = "Orrong Rd";
    s.persist(org);
    s.persist(a2);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    s.get(Entity.class, "IFA");
    s.clear();
    List list = s.createQuery("from Entity e order by e.name").list();
    p = (Person) list.get(0);
    assertNotNull(p.address);
    assertNull(p.mailingAddress);
    list.get(1);
    s.clear();
    list = s.createQuery("from Entity e left join fetch e.address left join fetch e.mailingAddress order by e.name").list();
    p = (Person) list.get(0);
    org = (Org) list.get(1);
    assertNotNull(p.address);
    assertNull(p.mailingAddress);
    s.clear();
    s.delete(p);
    s.delete(p.address);
    s.delete(org);
    s.delete(a2);
    s.flush();
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) List(java.util.List) EntityStatistics(org.hibernate.stat.EntityStatistics) Session(org.hibernate.Session) Test(org.junit.Test)

Example 3 with EntityStatistics

use of org.hibernate.stat.EntityStatistics in project hibernate-orm by hibernate.

the class DiscrimSubclassOneToOneTest method testOneToOneOnSubclass.

@Test
public void testOneToOneOnSubclass() {
    Person p = new Person();
    p.name = "Gavin";
    Address a = new Address();
    a.entityName = "Gavin";
    a.zip = "3181";
    a.state = "VIC";
    a.street = "Karbarook Ave";
    p.address = a;
    Session s = openSession();
    Transaction t = s.beginTransaction();
    s.persist(p);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    EntityStatistics addressStats = sessionFactory().getStatistics().getEntityStatistics(Address.class.getName());
    EntityStatistics mailingAddressStats = sessionFactory().getStatistics().getEntityStatistics("MailingAddress");
    p = (Person) s.createQuery("from Person p join fetch p.address left join fetch p.mailingAddress").uniqueResult();
    assertNotNull(p.address);
    assertNull(p.mailingAddress);
    s.clear();
    assertEquals(addressStats.getFetchCount(), 0);
    assertEquals(mailingAddressStats.getFetchCount(), 0);
    p = (Person) s.createQuery("from Person p join fetch p.address").uniqueResult();
    assertNotNull(p.address);
    assertNull(p.mailingAddress);
    s.clear();
    assertEquals(addressStats.getFetchCount(), 0);
    assertEquals(mailingAddressStats.getFetchCount(), 1);
    p = (Person) s.createQuery("from Person").uniqueResult();
    assertNotNull(p.address);
    assertNull(p.mailingAddress);
    s.clear();
    assertEquals(addressStats.getFetchCount(), 1);
    assertEquals(mailingAddressStats.getFetchCount(), 2);
    p = (Person) s.createQuery("from Entity").uniqueResult();
    assertNotNull(p.address);
    assertNull(p.mailingAddress);
    s.clear();
    assertEquals(addressStats.getFetchCount(), 2);
    assertEquals(mailingAddressStats.getFetchCount(), 3);
    //note that in here join fetch is used for the nullable
    //one-to-one, due to a very special case of default
    p = (Person) s.get(Person.class, "Gavin");
    assertNotNull(p.address);
    assertNull(p.mailingAddress);
    s.clear();
    assertEquals(addressStats.getFetchCount(), 2);
    assertEquals(mailingAddressStats.getFetchCount(), 3);
    p = (Person) s.get(Entity.class, "Gavin");
    assertNotNull(p.address);
    assertNull(p.mailingAddress);
    s.clear();
    assertEquals(addressStats.getFetchCount(), 2);
    assertEquals(mailingAddressStats.getFetchCount(), 3);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    Org org = new Org();
    org.name = "IFA";
    Address a2 = new Address();
    a2.entityName = "IFA";
    a2.zip = "3181";
    a2.state = "VIC";
    a2.street = "Orrong Rd";
    org.addresses.add(a2);
    s.persist(org);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    org = (Org) s.get(Entity.class, "IFA");
    s.clear();
    List list = s.createQuery("from Entity e order by e.name").list();
    p = (Person) list.get(0);
    assertNotNull(p.address);
    assertNull(p.mailingAddress);
    org = (Org) list.get(1);
    assertEquals(org.addresses.size(), 1);
    s.clear();
    list = s.createQuery("from Entity e left join fetch e.address left join fetch e.mailingAddress order by e.name").list();
    p = (Person) list.get(0);
    org = (Org) list.get(1);
    assertNotNull(p.address);
    assertNull(p.mailingAddress);
    assertEquals(org.addresses.size(), 1);
    s.delete(p);
    s.delete(org);
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) List(java.util.List) EntityStatistics(org.hibernate.stat.EntityStatistics) Session(org.hibernate.Session) Test(org.junit.Test)

Example 4 with EntityStatistics

use of org.hibernate.stat.EntityStatistics in project hibernate-orm by hibernate.

the class QueryCacheTest method testQueryCacheInvalidation.

@Test
public void testQueryCacheInvalidation() throws Exception {
    sessionFactory().getCache().evictQueryRegions();
    sessionFactory().getStatistics().clear();
    final String queryString = "from Item i where i.name='widget'";
    Session s = openSession();
    Transaction t = s.beginTransaction();
    s.createQuery(queryString).setCacheable(true).list();
    Item i = new Item();
    i.setName("widget");
    i.setDescription("A really top-quality, full-featured widget.");
    s.save(i);
    t.commit();
    s.close();
    QueryStatistics qs = s.getSessionFactory().getStatistics().getQueryStatistics(queryString);
    EntityStatistics es = s.getSessionFactory().getStatistics().getEntityStatistics(Item.class.getName());
    Thread.sleep(200);
    s = openSession();
    t = s.beginTransaction();
    List result = s.createQuery(queryString).setCacheable(true).list();
    assertEquals(result.size(), 1);
    t.commit();
    s.close();
    assertEquals(qs.getCacheHitCount(), 0);
    s = openSession();
    t = s.beginTransaction();
    result = s.createQuery(queryString).setCacheable(true).list();
    assertEquals(result.size(), 1);
    t.commit();
    s.close();
    assertEquals(qs.getCacheHitCount(), 1);
    assertEquals(s.getSessionFactory().getStatistics().getEntityFetchCount(), 0);
    s = openSession();
    t = s.beginTransaction();
    result = s.createQuery(queryString).setCacheable(true).list();
    assertEquals(result.size(), 1);
    assertTrue(Hibernate.isInitialized(result.get(0)));
    i = (Item) result.get(0);
    i.setName("Widget");
    t.commit();
    s.close();
    assertEquals(qs.getCacheHitCount(), 2);
    assertEquals(qs.getCacheMissCount(), 2);
    assertEquals(s.getSessionFactory().getStatistics().getEntityFetchCount(), 0);
    Thread.sleep(200);
    s = openSession();
    t = s.beginTransaction();
    result = s.createQuery(queryString).setCacheable(true).list();
    i = (Item) s.get(Item.class, new Long(i.getId()));
    s.delete(i);
    t.commit();
    s.close();
    assertEquals(qs.getCacheHitCount(), 2);
    assertEquals(qs.getCacheMissCount(), 3);
    assertEquals(qs.getCachePutCount(), 3);
    assertEquals(qs.getExecutionCount(), 3);
    //check that it was being cached
    assertEquals(es.getFetchCount(), 0);
}
Also used : Transaction(org.hibernate.Transaction) QueryStatistics(org.hibernate.stat.QueryStatistics) ArrayList(java.util.ArrayList) List(java.util.List) EntityStatistics(org.hibernate.stat.EntityStatistics) Session(org.hibernate.Session) Test(org.junit.Test)

Example 5 with EntityStatistics

use of org.hibernate.stat.EntityStatistics in project hibernate-orm by hibernate.

the class QueryCacheTest method testProjectionCache.

@Test
public void testProjectionCache() throws Exception {
    sessionFactory().getCache().evictQueryRegions();
    sessionFactory().getStatistics().clear();
    final String queryString = "select i.description as desc from Item i where i.name='widget'";
    Session s = openSession();
    Transaction t = s.beginTransaction();
    s.createQuery(queryString).setCacheable(true).list();
    Item i = new Item();
    i.setName("widget");
    i.setDescription("A really top-quality, full-featured widget.");
    s.save(i);
    t.commit();
    s.close();
    QueryStatistics qs = s.getSessionFactory().getStatistics().getQueryStatistics(queryString);
    EntityStatistics es = s.getSessionFactory().getStatistics().getEntityStatistics(Item.class.getName());
    assertEquals(qs.getCacheHitCount(), 0);
    assertEquals(qs.getCacheMissCount(), 1);
    assertEquals(qs.getCachePutCount(), 1);
    Thread.sleep(200);
    s = openSession();
    t = s.beginTransaction();
    List result = s.createQuery(queryString).setCacheable(true).list();
    assertEquals(result.size(), 1);
    assertEquals(i.getDescription(), (result.get(0)));
    t.commit();
    s.close();
    assertEquals(qs.getCacheHitCount(), 0);
    assertEquals(qs.getCacheMissCount(), 2);
    assertEquals(qs.getCachePutCount(), 2);
    s = openSession();
    t = s.beginTransaction();
    result = s.createQuery(queryString).setCacheable(true).list();
    assertEquals(result.size(), 1);
    assertEquals(i.getDescription(), result.get(0));
    t.commit();
    s.close();
    assertEquals(qs.getCacheHitCount(), 1);
    assertEquals(qs.getCacheMissCount(), 2);
    assertEquals(qs.getCachePutCount(), 2);
    s = openSession();
    t = s.beginTransaction();
    result = s.createQuery(queryString).setCacheable(true).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
    assertEquals(result.size(), 1);
    Map m = (Map) result.get(0);
    assertEquals(1, m.size());
    assertEquals(i.getDescription(), m.get("desc"));
    t.commit();
    s.close();
    assertEquals("hit count should go up since data is not transformed until afterQuery it is cached", qs.getCacheHitCount(), 2);
    assertEquals(qs.getCacheMissCount(), 2);
    assertEquals(qs.getCachePutCount(), 2);
    s = openSession();
    t = s.beginTransaction();
    result = s.createQuery(queryString).setCacheable(true).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
    assertEquals(result.size(), 1);
    m = (Map) result.get(0);
    assertEquals(1, m.size());
    assertEquals(i.getDescription(), m.get("desc"));
    t.commit();
    s.close();
    assertEquals("hit count should go up since data is not transformed until afterQuery it is cachedr", qs.getCacheHitCount(), 3);
    assertEquals(qs.getCacheMissCount(), 2);
    assertEquals(qs.getCachePutCount(), 2);
    s = openSession();
    t = s.beginTransaction();
    result = s.createQuery(queryString).setCacheable(true).list();
    assertEquals(result.size(), 1);
    assertTrue(Hibernate.isInitialized(result.get(0)));
    i = (Item) s.get(Item.class, new Long(i.getId()));
    i.setName("widget");
    i.setDescription("A middle-quality widget.");
    t.commit();
    s.close();
    assertEquals(qs.getCacheHitCount(), 4);
    assertEquals(qs.getCacheMissCount(), 2);
    assertEquals(qs.getCachePutCount(), 2);
    Thread.sleep(200);
    s = openSession();
    t = s.beginTransaction();
    result = s.createQuery(queryString).setCacheable(true).list();
    assertEquals(result.size(), 1);
    i = (Item) s.get(Item.class, new Long(i.getId()));
    assertEquals(result.get(0), "A middle-quality widget.");
    assertEquals(qs.getCacheHitCount(), 4);
    assertEquals(qs.getCacheMissCount(), 3);
    assertEquals(qs.getCachePutCount(), 3);
    s.delete(i);
    t.commit();
    s.close();
    assertEquals(qs.getCacheHitCount(), 4);
    assertEquals(qs.getCacheMissCount(), 3);
    assertEquals(qs.getCachePutCount(), 3);
    assertEquals(qs.getExecutionCount(), 3);
    //check that it was being cached
    assertEquals(es.getFetchCount(), 0);
}
Also used : Transaction(org.hibernate.Transaction) QueryStatistics(org.hibernate.stat.QueryStatistics) ArrayList(java.util.ArrayList) List(java.util.List) EntityStatistics(org.hibernate.stat.EntityStatistics) Map(java.util.Map) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

Session (org.hibernate.Session)5 EntityStatistics (org.hibernate.stat.EntityStatistics)5 Test (org.junit.Test)5 List (java.util.List)4 Transaction (org.hibernate.Transaction)4 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 QueryStatistics (org.hibernate.stat.QueryStatistics)2 HashMap (java.util.HashMap)1