use of org.hibernate.StatelessSession in project hibernate-orm by hibernate.
the class StatelessSessionTest method testRefresh.
@Test
public void testRefresh() {
StatelessSession ss = sessionFactory().openStatelessSession();
Transaction tx = ss.beginTransaction();
Paper paper = new Paper();
paper.setColor("whtie");
ss.insert(paper);
tx.commit();
ss.close();
ss = sessionFactory().openStatelessSession();
tx = ss.beginTransaction();
Paper p2 = (Paper) ss.get(Paper.class, paper.getId());
p2.setColor("White");
ss.update(p2);
tx.commit();
ss.close();
ss = sessionFactory().openStatelessSession();
tx = ss.beginTransaction();
assertEquals("whtie", paper.getColor());
ss.refresh(paper);
assertEquals("White", paper.getColor());
ss.delete(paper);
tx.commit();
ss.close();
}
use of org.hibernate.StatelessSession in project hibernate-orm by hibernate.
the class StatelessSessionTest method testHqlBulk.
@Test
public void testHqlBulk() {
StatelessSession ss = sessionFactory().openStatelessSession();
Transaction tx = ss.beginTransaction();
Document doc = new Document("blah blah blah", "Blahs");
ss.insert(doc);
Paper paper = new Paper();
paper.setColor("White");
ss.insert(paper);
tx.commit();
tx = ss.beginTransaction();
int count = ss.createQuery("update Document set name = :newName where name = :oldName").setString("newName", "Foos").setString("oldName", "Blahs").executeUpdate();
assertEquals("hql-update on stateless session", 1, count);
count = ss.createQuery("update Paper set color = :newColor").setString("newColor", "Goldenrod").executeUpdate();
assertEquals("hql-update on stateless session", 1, count);
tx.commit();
tx = ss.beginTransaction();
count = ss.createQuery("delete Document").executeUpdate();
assertEquals("hql-delete on stateless session", 1, count);
count = ss.createQuery("delete Paper").executeUpdate();
assertEquals("hql-delete on stateless session", 1, count);
tx.commit();
ss.close();
}
use of org.hibernate.StatelessSession in project hibernate-orm by hibernate.
the class StatelessSessionFetchingTest method testDynamicFetch.
@Test
public void testDynamicFetch() {
Session s = openSession();
s.beginTransaction();
Date now = new Date();
User me = new User("me");
User you = new User("you");
Resource yourClock = new Resource("clock", you);
// :)
Task task = new Task(me, "clean", yourClock, now);
s.save(me);
s.save(you);
s.save(yourClock);
s.save(task);
s.getTransaction().commit();
s.close();
StatelessSession ss = sessionFactory().openStatelessSession();
ss.beginTransaction();
Task taskRef = (Task) ss.createQuery("from Task t join fetch t.resource join fetch t.user").uniqueResult();
assertTrue(taskRef != null);
assertTrue(Hibernate.isInitialized(taskRef));
assertTrue(Hibernate.isInitialized(taskRef.getUser()));
assertTrue(Hibernate.isInitialized(taskRef.getResource()));
assertFalse(Hibernate.isInitialized(taskRef.getResource().getOwner()));
ss.getTransaction().commit();
ss.close();
cleanup();
}
use of org.hibernate.StatelessSession 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.StatelessSession in project hibernate-orm by hibernate.
the class StatelessSessionNativeQueryInsertTest method testInsertInStatelessSession.
@Test
@TestForIssue(jiraKey = "HHH-12141")
public void testInsertInStatelessSession() throws Exception {
doInHibernate(this::sessionFactory, session -> {
session.doWork(connection -> {
StatelessSession sls = sessionFactory().openStatelessSession(connection);
NativeQuery q = sls.createNativeQuery("INSERT INTO TEST_ENTITY (ID,SIMPLE_ATTRIBUTE) values (1,'red')");
q.executeUpdate();
});
});
}
Aggregations