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();
});
}
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();
}
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();
}
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();
}
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();
}
Aggregations