Search in sources :

Example 51 with Session

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

the class TernaryTest method testTernary.

@Test
public void testTernary() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Employee bob = new Employee("Bob");
    Employee tom = new Employee("Tom");
    Employee jim = new Employee("Jim");
    Employee tim = new Employee("Tim");
    Site melb = new Site("Melbourne");
    Site geel = new Site("Geelong");
    s.persist(bob);
    s.persist(tom);
    s.persist(jim);
    s.persist(tim);
    s.persist(melb);
    s.persist(geel);
    bob.getManagerBySite().put(melb, tom);
    bob.getManagerBySite().put(geel, jim);
    tim.getManagerBySite().put(melb, tom);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    tom = (Employee) s.get(Employee.class, "Tom");
    assertFalse(Hibernate.isInitialized(tom.getUnderlings()));
    assertEquals(tom.getUnderlings().size(), 2);
    bob = (Employee) s.get(Employee.class, "Bob");
    assertFalse(Hibernate.isInitialized(bob.getManagerBySite()));
    assertTrue(tom.getUnderlings().contains(bob));
    melb = (Site) s.get(Site.class, "Melbourne");
    assertSame(bob.getManagerBySite().get(melb), tom);
    assertTrue(melb.getEmployees().contains(bob));
    assertTrue(melb.getManagers().contains(tom));
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    List l = s.createQuery("from Employee e join e.managerBySite m where m.name='Bob'").list();
    assertEquals(l.size(), 0);
    l = s.createQuery("from Employee e join e.managerBySite m where m.name='Tom'").list();
    assertEquals(l.size(), 2);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    l = s.createQuery("from Employee e left join fetch e.managerBySite").list();
    assertEquals(l.size(), 5);
    Set set = new HashSet(l);
    assertEquals(set.size(), 4);
    Iterator iter = set.iterator();
    int total = 0;
    while (iter.hasNext()) {
        Map map = ((Employee) iter.next()).getManagerBySite();
        assertTrue(Hibernate.isInitialized(map));
        total += map.size();
    }
    assertTrue(total == 3);
    l = s.createQuery("from Employee e left join e.managerBySite m left join m.managerBySite m2").list();
    // clean up...
    l = s.createQuery("from Employee e left join fetch e.managerBySite").list();
    Iterator itr = l.iterator();
    while (itr.hasNext()) {
        Employee emp = (Employee) itr.next();
        emp.setManagerBySite(new HashMap());
        s.delete(emp);
    }
    for (Object entity : s.createQuery("from Site").list()) {
        s.delete(entity);
    }
    t.commit();
    s.close();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Transaction(org.hibernate.Transaction) HashMap(java.util.HashMap) Iterator(java.util.Iterator) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Session(org.hibernate.Session) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 52 with Session

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

the class NativeQuerySyncSpaceCachingTest method after.

@After
public void after() {
    Session session = sessionFactory().openSession();
    session.beginTransaction();
    session.createQuery("delete Customer").executeUpdate();
    session.getTransaction().commit();
    session.close();
}
Also used : Session(org.hibernate.Session) After(org.junit.After)

Example 53 with Session

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

the class NativeQuerySyncSpaceCachingTest method before.

@Before
public void before() {
    Session session = sessionFactory().openSession();
    session.beginTransaction();
    Customer customer = new Customer(1, "Samuel");
    session.saveOrUpdate(customer);
    session.getTransaction().commit();
    session.close();
}
Also used : Session(org.hibernate.Session) Before(org.junit.Before)

Example 54 with Session

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

the class NativeQuerySyncSpaceCachingTest method testSelectCachedEntityWithNoSyncSpaces.

@Test
public void testSelectCachedEntityWithNoSyncSpaces() {
    assertTrue(sessionFactory().getCache().containsEntity(Customer.class, 1));
    Session session = openSession();
    session.createSQLQuery("select * from Customer").list();
    session.close();
    assertTrue(sessionFactory().getCache().containsEntity(Customer.class, 1));
}
Also used : Session(org.hibernate.Session) Test(org.junit.Test)

Example 55 with Session

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

the class BinaryTypeTest method testByteArrayStringRepresentation.

@Test
public void testByteArrayStringRepresentation() {
    Session s = openSession();
    s.getTransaction().begin();
    try {
        Image image = new Image();
        image.id = 1L;
        image.content = new byte[] { 1, 2, 3 };
        s.save(image);
        s.getTransaction().commit();
    } catch (Exception e) {
        if (s.getTransaction() != null && s.getTransaction().getStatus() == TransactionStatus.ACTIVE) {
            s.getTransaction().rollback();
        }
        fail(e.getMessage());
    } finally {
        s.close();
    }
    s = openSession();
    s.getTransaction().begin();
    try {
        assertArrayEquals(new byte[] { 1, 2, 3 }, s.find(Image.class, 1L).content);
        s.getTransaction().commit();
    } catch (Exception e) {
        if (s.getTransaction() != null && s.getTransaction().getStatus() == TransactionStatus.ACTIVE) {
            s.getTransaction().rollback();
        }
        fail(e.getMessage());
    } finally {
        s.close();
    }
}
Also used : Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

Session (org.hibernate.Session)4457 Test (org.junit.Test)2517 Transaction (org.hibernate.Transaction)1327 List (java.util.List)576 ArrayList (java.util.ArrayList)475 Test (org.testng.annotations.Test)411 TestForIssue (org.hibernate.testing.TestForIssue)389 Query (org.hibernate.Query)345 HibernateException (org.hibernate.HibernateException)299 DAOException (com.tomasio.projects.trainning.exception.DAOException)186 Criteria (org.hibernate.Criteria)174 Date (java.util.Date)134 Iterator (java.util.Iterator)127 SkipForDialect (org.hibernate.testing.SkipForDialect)114 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)113 SQLException (java.sql.SQLException)108 Map (java.util.Map)105 SessionFactory (org.hibernate.SessionFactory)105 GradingFactorDaoException (com.remswork.project.alice.dao.exception.GradingFactorDaoException)104 GradingFactorException (com.remswork.project.alice.exception.GradingFactorException)104