Search in sources :

Example 1 with Distributor

use of org.hibernate.jpa.test.Distributor in project hibernate-orm by hibernate.

the class QueryTest method testDistinct.

@Test
public void testDistinct() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    try {
        em.createQuery("delete Item").executeUpdate();
        em.createQuery("delete Distributor").executeUpdate();
        Distributor d1 = new Distributor();
        d1.setName("Fnac");
        Distributor d2 = new Distributor();
        d2.setName("Darty");
        Item item = new Item("Mouse", "Micro$oft mouse");
        item.getDistributors().add(d1);
        item.getDistributors().add(d2);
        em.persist(d1);
        em.persist(d2);
        em.persist(item);
        em.flush();
        em.clear();
        Query q = em.createQuery("select distinct i from Item i left join fetch i.distributors");
        item = (Item) q.getSingleResult();
        //assertEquals( 1, distinctResult.size() );
        //item = (Item) distinctResult.get( 0 );
        assertTrue(Hibernate.isInitialized(item.getDistributors()));
        assertEquals(2, item.getDistributors().size());
    } finally {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }
}
Also used : Item(org.hibernate.jpa.test.Item) EntityManager(javax.persistence.EntityManager) Distributor(org.hibernate.jpa.test.Distributor) Query(javax.persistence.Query) Test(org.junit.Test)

Example 2 with Distributor

use of org.hibernate.jpa.test.Distributor in project hibernate-orm by hibernate.

the class PackagedEntityManagerTest method testConfiguration.

@Test
public void testConfiguration() throws Exception {
    File testPackage = buildExplicitPar();
    addPackageToClasspath(testPackage);
    emf = Persistence.createEntityManagerFactory("manager1", new HashMap());
    Item item = new Item("Mouse", "Micro$oft mouse");
    Distributor res = new Distributor();
    res.setName("Bruce");
    item.setDistributors(new HashSet<Distributor>());
    item.getDistributors().add(res);
    Statistics stats = ((HibernateEntityManagerFactory) emf).getSessionFactory().getStatistics();
    stats.clear();
    stats.setStatisticsEnabled(true);
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    em.persist(res);
    em.persist(item);
    assertTrue(em.contains(item));
    em.getTransaction().commit();
    em.close();
    assertEquals(1, stats.getSecondLevelCachePutCount());
    assertEquals(0, stats.getSecondLevelCacheHitCount());
    em = emf.createEntityManager();
    em.getTransaction().begin();
    Item second = em.find(Item.class, item.getName());
    assertEquals(1, second.getDistributors().size());
    assertEquals(1, stats.getSecondLevelCacheHitCount());
    em.getTransaction().commit();
    em.close();
    em = emf.createEntityManager();
    em.getTransaction().begin();
    second = em.find(Item.class, item.getName());
    assertEquals(1, second.getDistributors().size());
    assertEquals(3, stats.getSecondLevelCacheHitCount());
    for (Distributor distro : second.getDistributors()) {
        em.remove(distro);
    }
    em.remove(second);
    em.getTransaction().commit();
    em.close();
    stats.clear();
    stats.setStatisticsEnabled(false);
    emf.close();
}
Also used : Item(org.hibernate.jpa.test.Item) EntityManager(javax.persistence.EntityManager) Distributor(org.hibernate.jpa.test.Distributor) HashMap(java.util.HashMap) File(java.io.File) Statistics(org.hibernate.stat.Statistics) Test(org.junit.Test)

Example 3 with Distributor

use of org.hibernate.jpa.test.Distributor in project hibernate-orm by hibernate.

the class QueryTest method testIsNull.

@Test
public void testIsNull() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    try {
        Distributor d1 = new Distributor();
        d1.setName("Fnac");
        Distributor d2 = new Distributor();
        d2.setName("Darty");
        Item item = new Item("Mouse", null);
        Item item2 = new Item("Mouse2", "dd");
        item.getDistributors().add(d1);
        item.getDistributors().add(d2);
        em.persist(d1);
        em.persist(d2);
        em.persist(item);
        em.persist(item2);
        em.flush();
        em.clear();
        Query q = em.createQuery("select i from Item i where i.descr = :descr or (i.descr is null and cast(:descr as string) is null)");
        //Query q = em.createQuery( "select i from Item i where (i.descr is null and :descr is null) or (i.descr = :descr");
        q.setParameter("descr", "dd");
        List result = q.getResultList();
        assertEquals(1, result.size());
        q.setParameter("descr", null);
        result = q.getResultList();
        assertEquals(1, result.size());
    //item = (Item) distinctResult.get( 0 );
    } finally {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }
}
Also used : Item(org.hibernate.jpa.test.Item) EntityManager(javax.persistence.EntityManager) Distributor(org.hibernate.jpa.test.Distributor) Query(javax.persistence.Query) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

EntityManager (javax.persistence.EntityManager)3 Distributor (org.hibernate.jpa.test.Distributor)3 Item (org.hibernate.jpa.test.Item)3 Test (org.junit.Test)3 Query (javax.persistence.Query)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Statistics (org.hibernate.stat.Statistics)1