Search in sources :

Example 1 with Transaction

use of org.hibernate.Transaction in project dropwizard by dropwizard.

the class SessionFactoryHealthCheck method check.

@Override
protected Result check() throws Exception {
    return timeBoundHealthCheck.check(() -> {
        try (Session session = sessionFactory.openSession()) {
            final Transaction txn = session.beginTransaction();
            try {
                session.createNativeQuery(validationQuery).list();
                txn.commit();
            } catch (Exception e) {
                if (txn.getStatus().canRollback()) {
                    txn.rollback();
                }
                throw e;
            }
        }
        return Result.healthy();
    });
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session)

Example 2 with Transaction

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

the class UnionSubclassTest method testUnionSubclassFetchMode.

@Test
public void testUnionSubclassFetchMode() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Location mel = new Location("Earth");
    s.save(mel);
    Human gavin = new Human();
    gavin.setIdentity("gavin");
    gavin.setSex('M');
    gavin.setLocation(mel);
    mel.addBeing(gavin);
    Human max = new Human();
    max.setIdentity("max");
    max.setSex('M');
    max.setLocation(mel);
    mel.addBeing(gavin);
    s.flush();
    s.clear();
    List list = s.createCriteria(Human.class).setFetchMode("location", FetchMode.JOIN).setFetchMode("location.beings", FetchMode.JOIN).list();
    for (Object aList : list) {
        Human h = (Human) aList;
        assertTrue(Hibernate.isInitialized(h.getLocation()));
        assertTrue(Hibernate.isInitialized(h.getLocation().getBeings()));
        s.delete(h);
    }
    s.delete(s.get(Location.class, mel.getId()));
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Example 3 with Transaction

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

the class SellCarTest method prepareData.

private void prepareData() {
    Session session = openSession();
    Transaction tx = session.beginTransaction();
    session.save(createData());
    tx.commit();
    session.close();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session)

Example 4 with Transaction

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

the class SellCarTest method testSellCar.

@Test
public void testSellCar() throws Exception {
    prepareData();
    Session session = openSession();
    Transaction tx = session.beginTransaction();
    Query query = session.createQuery("from Seller");
    Seller seller = (Seller) query.uniqueResult();
    assertNotNull(seller);
    assertEquals(1, seller.getBuyers().size());
    tx.commit();
    session.close();
}
Also used : Transaction(org.hibernate.Transaction) Query(org.hibernate.Query) Session(org.hibernate.Session) Test(org.junit.Test)

Example 5 with Transaction

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

the class UnionSubclassTest method testUnionSubclass.

@Test
@SkipForDialect(value = TeradataDialect.class, jiraKey = "HHH-8190", comment = "SQL uses Teradata reserved word: title")
public void testUnionSubclass() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Employee mark = new Employee();
    mark.setName("Mark");
    mark.setTitle("internal sales");
    mark.setSex('M');
    mark.setAddress("buckhead");
    mark.setZip("30305");
    mark.setCountry("USA");
    Customer joe = new Customer();
    joe.setName("Joe");
    joe.setAddress("San Francisco");
    joe.setZip("XXXXX");
    joe.setCountry("USA");
    joe.setComments("Very demanding");
    joe.setSex('M');
    joe.setSalesperson(mark);
    Person yomomma = new Person();
    yomomma.setName("mum");
    yomomma.setSex('F');
    s.save(yomomma);
    s.save(mark);
    s.save(joe);
    assertEquals(s.createQuery("from java.io.Serializable").list().size(), 0);
    assertEquals(s.createQuery("from Person").list().size(), 3);
    assertEquals(s.createQuery("from Person p where p.class = Customer").list().size(), 1);
    assertEquals(s.createQuery("from Person p where p.class = Person").list().size(), 1);
    assertEquals(s.createQuery("from Person p where type(p) in :who").setParameter("who", Customer.class).list().size(), 1);
    assertEquals(s.createQuery("from Person p where type(p) in :who").setParameterList("who", new Class[] { Customer.class, Person.class }).list().size(), 2);
    s.clear();
    List customers = s.createQuery("from Customer c left join fetch c.salesperson").list();
    for (Object customer : customers) {
        Customer c = (Customer) customer;
        assertTrue(Hibernate.isInitialized(c.getSalesperson()));
        assertEquals(c.getSalesperson().getName(), "Mark");
    }
    assertEquals(customers.size(), 1);
    s.clear();
    customers = s.createQuery("from Customer").list();
    for (Object customer : customers) {
        Customer c = (Customer) customer;
        assertFalse(Hibernate.isInitialized(c.getSalesperson()));
        assertEquals(c.getSalesperson().getName(), "Mark");
    }
    assertEquals(customers.size(), 1);
    s.clear();
    mark = (Employee) s.get(Employee.class, Long.valueOf(mark.getId()));
    joe = (Customer) s.get(Customer.class, Long.valueOf(joe.getId()));
    mark.setZip("30306");
    assertEquals(s.createQuery("from Person p where p.address.zip = '30306'").list().size(), 1);
    s.createCriteria(Person.class).add(Restrictions.in("address", new Address[] { mark.getAddress(), joe.getAddress() })).list();
    s.delete(mark);
    s.delete(joe);
    s.delete(yomomma);
    assertTrue(s.createQuery("from Person").list().isEmpty());
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) List(java.util.List) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Aggregations

Transaction (org.hibernate.Transaction)1393 Session (org.hibernate.Session)1322 Test (org.junit.Test)1131 List (java.util.List)235 ArrayList (java.util.ArrayList)144 TestForIssue (org.hibernate.testing.TestForIssue)88 Query (org.hibernate.Query)86 Iterator (java.util.Iterator)85 BigDecimal (java.math.BigDecimal)73 SkipForDialect (org.hibernate.testing.SkipForDialect)73 TransactionUtil2.inTransaction (org.hibernate.testing.transaction.TransactionUtil2.inTransaction)64 Date (java.util.Date)54 HashSet (java.util.HashSet)44 HibernateException (org.hibernate.HibernateException)42 PersistenceException (javax.persistence.PersistenceException)41 Criteria (org.hibernate.Criteria)41 ScrollableResults (org.hibernate.ScrollableResults)36 HashMap (java.util.HashMap)30 Set (java.util.Set)30 Map (java.util.Map)26