Search in sources :

Example 11 with Transaction

use of org.hibernate.Transaction in project hibernate-orm by hibernate.

the class TestSpatialFunctions method doInSession.

private <T> void doInSession(String hql, Map<Integer, T> result, Map<String, Object> params) {
    Session session = null;
    Transaction tx = null;
    try {
        session = openSession();
        tx = session.beginTransaction();
        Query query = session.createQuery(hql);
        setParameters(params, query);
        addQueryResults(result, query);
    } finally {
        if (tx != null) {
            tx.rollback();
        }
        if (session != null) {
            session.close();
        }
    }
}
Also used : Transaction(org.hibernate.Transaction) Query(org.hibernate.Query) Session(org.hibernate.Session)

Example 12 with Transaction

use of org.hibernate.Transaction in project hibernate-orm by hibernate.

the class TestSpatialRestrictions method retrieveAndCompare.

private void retrieveAndCompare(Map<Integer, Boolean> dbexpected, Criterion spatialCriterion) {
    Session session = null;
    Transaction tx = null;
    try {
        session = openSession();
        tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(GeomEntity.class);
        criteria.add(spatialCriterion);
        compare(dbexpected, criteria.list());
    } finally {
        if (tx != null) {
            tx.rollback();
        }
        if (session != null) {
            session.close();
        }
    }
}
Also used : Transaction(org.hibernate.Transaction) Criteria(org.hibernate.Criteria) Session(org.hibernate.Session)

Example 13 with Transaction

use of org.hibernate.Transaction in project hibernate-orm by hibernate.

the class DiscrimSubclassFilterTest method testFiltersWithSubclass.

@Test
@SuppressWarnings({ "unchecked" })
public void testFiltersWithSubclass() {
    Session s = openSession();
    s.enableFilter("region").setParameter("userRegion", "US");
    Transaction t = s.beginTransaction();
    prepareTestData(s);
    s.clear();
    List results;
    Iterator itr;
    results = s.createQuery("from Person").list();
    assertEquals("Incorrect qry result count", 4, results.size());
    s.clear();
    results = s.createQuery("from Employee").list();
    assertEquals("Incorrect qry result count", 2, results.size());
    s.clear();
    results = new ArrayList(new HashSet(s.createQuery("from Person as p left join fetch p.minions").list()));
    assertEquals("Incorrect qry result count", 4, results.size());
    itr = results.iterator();
    while (itr.hasNext()) {
        // find john
        final Person p = (Person) itr.next();
        if (p.getName().equals("John Doe")) {
            Employee john = (Employee) p;
            assertEquals("Incorrect fecthed minions count", 1, john.getMinions().size());
            break;
        }
    }
    s.clear();
    results = new ArrayList(new HashSet(s.createQuery("from Employee as p left join fetch p.minions").list()));
    assertEquals("Incorrect qry result count", 2, results.size());
    itr = results.iterator();
    while (itr.hasNext()) {
        // find john
        final Person p = (Person) itr.next();
        if (p.getName().equals("John Doe")) {
            Employee john = (Employee) p;
            assertEquals("Incorrect fecthed minions count", 1, john.getMinions().size());
            break;
        }
    }
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    s.createQuery("delete Customer where contactOwner is not null").executeUpdate();
    s.createQuery("delete Employee where manager is not null").executeUpdate();
    s.createQuery("delete Person").executeUpdate();
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Session(org.hibernate.Session) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 14 with Transaction

use of org.hibernate.Transaction in project hibernate-orm by hibernate.

the class JoinedSubclassFilterTest method testFiltersWithJoinedSubclass.

@Test
@SuppressWarnings({ "unchecked" })
public void testFiltersWithJoinedSubclass() {
    Session s = openSession();
    s.enableFilter("region").setParameter("userRegion", "US");
    Transaction t = s.beginTransaction();
    prepareTestData(s);
    s.clear();
    List results = s.createQuery("from Person").list();
    assertEquals("Incorrect qry result count", 4, results.size());
    s.clear();
    results = s.createQuery("from Employee").list();
    assertEquals("Incorrect qry result count", 2, results.size());
    Iterator itr = results.iterator();
    while (itr.hasNext()) {
        // find john
        final Person p = (Person) itr.next();
        if (p.getName().equals("John Doe")) {
            Employee john = (Employee) p;
            assertEquals("Incorrect fecthed minions count", 2, john.getMinions().size());
            break;
        }
    }
    s.clear();
    // TODO : currently impossible to define a collection-level filter w/ joined-subclass elements that will filter based on a superclass column and function correctly in (theta only?) outer joins;
    // this is consistent with the behaviour of a collection-level where.
    // this might be one argument for "pulling" the attached class-level filters into collection assocations,
    // although we'd need some way to apply the appropriate alias in that scenario.
    results = new ArrayList(new HashSet(s.createQuery("from Person as p left join fetch p.minions").list()));
    assertEquals("Incorrect qry result count", 4, results.size());
    itr = results.iterator();
    while (itr.hasNext()) {
        // find john
        final Person p = (Person) itr.next();
        if (p.getName().equals("John Doe")) {
            Employee john = (Employee) p;
            assertEquals("Incorrect fecthed minions count", 2, john.getMinions().size());
            break;
        }
    }
    s.clear();
    results = new ArrayList(new HashSet(s.createQuery("from Employee as p left join fetch p.minions").list()));
    assertEquals("Incorrect qry result count", 2, results.size());
    itr = results.iterator();
    while (itr.hasNext()) {
        // find john
        final Person p = (Person) itr.next();
        if (p.getName().equals("John Doe")) {
            Employee john = (Employee) p;
            assertEquals("Incorrect fecthed minions count", 2, john.getMinions().size());
            break;
        }
    }
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    s.createQuery("delete Customer where contactOwner is not null").executeUpdate();
    s.createQuery("delete Employee where manager is not null").executeUpdate();
    s.createQuery("delete Person").executeUpdate();
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Session(org.hibernate.Session) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 15 with Transaction

use of org.hibernate.Transaction in project hibernate-orm by hibernate.

the class SubselectTest method testEntitySubselect.

@Test
@SuppressWarnings({ "unchecked" })
public void testEntitySubselect() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Human gavin = new Human();
    gavin.setName("gavin");
    gavin.setSex('M');
    gavin.setAddress("Melbourne, Australia");
    Alien x23y4 = new Alien();
    x23y4.setIdentity("x23y4$$hu%3");
    x23y4.setPlanet("Mars");
    x23y4.setSpecies("martian");
    s.save(gavin);
    s.save(x23y4);
    s.flush();
    List<Being> beings = (List<Being>) s.createQuery("from Being").list();
    for (Being being : beings) {
        assertNotNull(being.getLocation());
        assertNotNull(being.getIdentity());
        assertNotNull(being.getSpecies());
    }
    s.clear();
    sessionFactory().getCache().evictEntityRegion(Being.class);
    Being gav = (Being) s.get(Being.class, gavin.getId());
    assertEquals(gav.getLocation(), gavin.getAddress());
    assertEquals(gav.getSpecies(), "human");
    assertEquals(gav.getIdentity(), gavin.getName());
    s.clear();
    //test the <synchronized> tag:
    gavin = (Human) s.get(Human.class, gavin.getId());
    gavin.setAddress("Atlanta, GA");
    gav = (Being) s.createQuery("from Being b where b.location like '%GA%'").uniqueResult();
    assertEquals(gav.getLocation(), gavin.getAddress());
    s.delete(gavin);
    s.delete(x23y4);
    assertTrue(s.createQuery("from Being").list().isEmpty());
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

Transaction (org.hibernate.Transaction)1272 Session (org.hibernate.Session)1250 Test (org.junit.Test)1124 List (java.util.List)239 ArrayList (java.util.ArrayList)143 Iterator (java.util.Iterator)87 TestForIssue (org.hibernate.testing.TestForIssue)87 Query (org.hibernate.Query)80 BigDecimal (java.math.BigDecimal)73 Date (java.util.Date)52 HashSet (java.util.HashSet)44 Criteria (org.hibernate.Criteria)39 SkipForDialect (org.hibernate.testing.SkipForDialect)36 ScrollableResults (org.hibernate.ScrollableResults)35 Set (java.util.Set)30 PersistenceException (javax.persistence.PersistenceException)30 HashMap (java.util.HashMap)29 Map (java.util.Map)25 FailureExpected (org.hibernate.testing.FailureExpected)23 Serializable (java.io.Serializable)22