Search in sources :

Example 1 with Session

use of org.hibernate.Session 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 Session

use of org.hibernate.Session 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 Session

use of org.hibernate.Session 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 Session

use of org.hibernate.Session 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 Session

use of org.hibernate.Session 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

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