use of org.hibernate.Session in project hibernate-orm by hibernate.
the class SubselectFetchWithFormulaTransactSqlTest method before.
@Before
public void before() {
Session session = openSession();
session.getTransaction().begin();
Name chris = new Name();
chris.setId(1);
chris.setName("chris");
Value cat = new Value();
cat.setId(1);
cat.setName(chris);
cat.setValue("cat");
Value canary = new Value();
canary.setId(2);
canary.setName(chris);
canary.setValue("canary");
session.persist(chris);
session.persist(cat);
session.persist(canary);
Name sam = new Name();
sam.setId(2);
sam.setName("sam");
Value seal = new Value();
seal.setId(3);
seal.setName(sam);
seal.setValue("seal");
Value snake = new Value();
snake.setId(4);
snake.setName(sam);
snake.setValue("snake");
session.persist(sam);
session.persist(seal);
session.persist(snake);
session.getTransaction().commit();
session.close();
}
use of org.hibernate.Session in project hibernate-orm by hibernate.
the class MySQL57TimestampFspFunctionTest method testTimeStampFunctions.
@Test
public void testTimeStampFunctions() {
// add an entity just so it can be queried.
Session s = openSession();
Transaction tx = s.beginTransaction();
s.persist(new Entity());
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
// current_timestamp(), localtime(), and localtimestamp() are synonyms for now(),
// which returns the time at which the statement began to execute.
// the returned values for now(), current_timestamp(), localtime(), and
// localtimestamp() should be the same.
// sysdate() is the time at which the function itself is executed, so the
// value returned for sysdate() should be different.
Query q = s.createQuery("select now(), current_timestamp(), localtime(), localtimestamp(), sysdate() from MySQL57TimestampFspFunctionTest$Entity");
Object[] oArray = (Object[]) q.uniqueResult();
final Timestamp now = (Timestamp) oArray[0];
assertEquals(now, oArray[1]);
assertEquals(now, oArray[2]);
assertEquals(now, oArray[3]);
final Timestamp sysdate = (Timestamp) oArray[4];
assertTrue(now.compareTo(sysdate) < 0);
// all should have nanos > 0
assertTrue(now.getNanos() > 0);
assertTrue(sysdate.getNanos() > 0);
tx.commit();
s.close();
}
use of org.hibernate.Session in project hibernate-orm by hibernate.
the class OffsetDateTimeTest method saveOffsetDateTimeEventWithStartDate.
private void saveOffsetDateTimeEventWithStartDate(OffsetDateTime startdate) {
final OffsetDateTimeEvent event = new OffsetDateTimeEvent();
event.id = 1L;
event.startDate = startdate;
final Session s = openSession();
s.getTransaction().begin();
try {
s.save(event);
s.getTransaction().commit();
} catch (Exception e) {
if (s.getTransaction() != null && s.getTransaction().getStatus() == TransactionStatus.ACTIVE) {
s.getTransaction().rollback();
}
fail(e.getMessage());
} finally {
s.close();
}
}
use of org.hibernate.Session in project hibernate-orm by hibernate.
the class OffsetDateTimeTest method compareSavedOffsetDateTimeWith.
private void compareSavedOffsetDateTimeWith(OffsetDateTime startdate) {
final Session s = openSession();
try {
final OffsetDateTimeEvent offsetDateEvent = s.get(OffsetDateTimeEvent.class, 1L);
assertThat(OffsetDateTimeType.INSTANCE.getComparator().compare(offsetDateEvent.startDate, startdate), is(0));
} finally {
s.close();
}
}
use of org.hibernate.Session in project hibernate-orm by hibernate.
the class SubselectInJoinedTableTest method testSubselectInJoinedTable.
@Test
@TestForIssue(jiraKey = "HHH-10998")
public void testSubselectInJoinedTable() {
OrderEntry orderEntry1 = new OrderEntry();
orderEntry1.setOrderEntryId(1L);
OrderEntry orderEntry2 = new OrderEntry();
orderEntry2.setOrderEntryId(2L);
Order order = new Order();
order.setOrderId(3L);
order.getOrderEntries().add(orderEntry1);
order.getOrderEntries().add(orderEntry2);
order.setFirstOrderEntry(orderEntry1);
Session s = openSession();
Transaction tx = s.beginTransaction();
s.persist(orderEntry1);
s.persist(orderEntry2);
s.persist(order);
tx.commit();
s.close();
s = openSession();
s.getTransaction().begin();
order = (Order) s.get(Order.class, order.getOrderId());
assertEquals(orderEntry1.getOrderEntryId(), order.getFirstOrderEntry().getOrderEntryId());
assertEquals(2, order.getOrderEntries().size());
assertEquals(orderEntry1.getOrderEntryId(), order.getOrderEntries().get(0).getOrderEntryId());
assertEquals(orderEntry2.getOrderEntryId(), order.getOrderEntries().get(1).getOrderEntryId());
s.getTransaction().commit();
s.close();
}
Aggregations