use of org.hibernate.Session in project hibernate-orm by hibernate.
the class StatelessSessionInsertTest method cleanup.
private void cleanup() {
Session s = openSession();
s.beginTransaction();
s.createQuery("delete MessageRecipient").executeUpdate();
s.createQuery("delete Message").executeUpdate();
s.getTransaction().commit();
s.close();
}
use of org.hibernate.Session in project hibernate-orm by hibernate.
the class StatelessSessionInsertTest method testInsertWithForeignKey.
@Test
public void testInsertWithForeignKey() {
Session session = sessionFactory().openSession();
Transaction tx = session.beginTransaction();
Message msg = new Message();
final String messageId = "message_id";
msg.setId(messageId);
msg.setContent("message_content");
msg.setSubject("message_subject");
session.save(msg);
tx.commit();
session.close();
StatelessSession statelessSession = sessionFactory().openStatelessSession();
tx = statelessSession.beginTransaction();
MessageRecipient signature = new MessageRecipient();
signature.setId("recipient");
signature.setEmail("recipient@hibernate.org");
signature.setMessage(msg);
statelessSession.insert(signature);
tx.commit();
cleanup();
}
use of org.hibernate.Session in project hibernate-orm by hibernate.
the class SessionStatsTest method testSessionStatistics.
@Test
public void testSessionStatistics() throws Exception {
Session s = openSession();
Transaction tx = s.beginTransaction();
Statistics stats = sessionFactory().getStatistics();
stats.clear();
boolean isStats = stats.isStatisticsEnabled();
stats.setStatisticsEnabled(true);
Continent europe = fillDb(s);
tx.commit();
s.clear();
tx = s.beginTransaction();
SessionStatistics sessionStats = s.getStatistics();
assertEquals(0, sessionStats.getEntityKeys().size());
assertEquals(0, sessionStats.getEntityCount());
assertEquals(0, sessionStats.getCollectionKeys().size());
assertEquals(0, sessionStats.getCollectionCount());
europe = (Continent) s.get(Continent.class, europe.getId());
Hibernate.initialize(europe.getCountries());
Hibernate.initialize(europe.getCountries().iterator().next());
assertEquals(2, sessionStats.getEntityKeys().size());
assertEquals(2, sessionStats.getEntityCount());
assertEquals(1, sessionStats.getCollectionKeys().size());
assertEquals(1, sessionStats.getCollectionCount());
tx.commit();
s.close();
stats.setStatisticsEnabled(isStats);
}
use of org.hibernate.Session in project hibernate-orm by hibernate.
the class BasicStreamTest method testQueryStream.
@Test
@TestForIssue(jiraKey = "HHH-10824")
public void testQueryStream() {
Session session = openSession();
try {
session.getTransaction().begin();
MyEntity e = new MyEntity();
e.id = 1;
e.name = "Test";
session.persist(e);
session.getTransaction().commit();
session.clear();
// Test stream query without type.
Object result = session.createQuery("From MyEntity").stream().findFirst().orElse(null);
assertTyping(MyEntity.class, result);
// Test stream query with type.
result = session.createQuery("From MyEntity", MyEntity.class).stream().findFirst().orElse(null);
assertTyping(MyEntity.class, result);
// Test stream query using forEach
session.createQuery("From MyEntity", MyEntity.class).stream().forEach(i -> {
assertTyping(MyEntity.class, i);
});
Stream<Object[]> data = session.createQuery("SELECT me.id, me.name FROM MyEntity me").stream();
data.forEach(i -> {
assertTyping(Integer.class, i[0]);
assertTyping(String.class, i[1]);
});
} finally {
session.close();
}
}
use of org.hibernate.Session in project hibernate-orm by hibernate.
the class MySQL57TimestampPropertyTest method testTimeGeneratedByColumnDefault.
@Test
public void testTimeGeneratedByColumnDefault() {
final Entity eOrig = new Entity();
Session s = openSession();
s.getTransaction().begin();
s.persist(eOrig);
s.getTransaction().commit();
s.close();
assertNotNull(eOrig.tsColumnDefault);
s = openSession();
s.getTransaction().begin();
final Entity eGotten = (Entity) s.get(Entity.class, eOrig.id);
final String tsColumnDefaultOrigFormatted = timestampFormat.format(eOrig.tsColumnDefault);
final String tsColumnDefaultGottenFormatted = timestampFormat.format(eGotten.tsColumnDefault);
assertEquals(tsColumnDefaultOrigFormatted, tsColumnDefaultGottenFormatted);
s.getTransaction().commit();
s.close();
s = openSession();
s.getTransaction().begin();
final Query queryWithParameter = s.createQuery("from MySQL57TimestampPropertyTest$Entity where tsColumnDefault=?").setParameter(0, eOrig.tsColumnDefault);
final Entity eQueriedWithParameter = (Entity) queryWithParameter.uniqueResult();
assertNotNull(eQueriedWithParameter);
s.getTransaction().commit();
s.close();
s = openSession();
s.getTransaction().begin();
final Query queryWithTimestamp = s.createQuery("from MySQL57TimestampPropertyTest$Entity where tsColumnDefault=?").setTimestamp(0, eOrig.tsColumnDefault);
final Entity eQueriedWithTimestamp = (Entity) queryWithTimestamp.uniqueResult();
assertNotNull(eQueriedWithTimestamp);
s.getTransaction().commit();
s.close();
s = openSession();
s.getTransaction().begin();
s.delete(eQueriedWithTimestamp);
s.getTransaction().commit();
s.close();
}
Aggregations