use of org.hibernate.Session in project hibernate-orm by hibernate.
the class SubselectFetchTest method testSubselectFetchWithLimit.
@Test
public void testSubselectFetchWithLimit() {
Session s = openSession();
Transaction t = s.beginTransaction();
Parent p = new Parent("foo");
p.getChildren().add(new Child("foo1"));
p.getChildren().add(new Child("foo2"));
Parent q = new Parent("bar");
q.getChildren().add(new Child("bar1"));
q.getChildren().add(new Child("bar2"));
Parent r = new Parent("aaa");
r.getChildren().add(new Child("aaa1"));
s.persist(p);
s.persist(q);
s.persist(r);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
sessionFactory().getStatistics().clear();
List parents = s.createQuery("from Parent order by name desc").setMaxResults(2).list();
p = (Parent) parents.get(0);
q = (Parent) parents.get(1);
assertFalse(Hibernate.isInitialized(p.getChildren()));
assertFalse(Hibernate.isInitialized(p.getMoreChildren()));
assertFalse(Hibernate.isInitialized(q.getChildren()));
assertFalse(Hibernate.isInitialized(q.getMoreChildren()));
assertEquals(p.getMoreChildren().size(), 0);
assertEquals(p.getChildren().size(), 2);
assertTrue(Hibernate.isInitialized(q.getChildren()));
assertTrue(Hibernate.isInitialized(q.getMoreChildren()));
assertEquals(3, sessionFactory().getStatistics().getPrepareStatementCount());
r = (Parent) s.get(Parent.class, r.getName());
assertTrue(Hibernate.isInitialized(r.getChildren()));
assertFalse(Hibernate.isInitialized(r.getMoreChildren()));
assertEquals(r.getChildren().size(), 1);
assertEquals(r.getMoreChildren().size(), 0);
s.delete(p);
s.delete(q);
s.delete(r);
t.commit();
s.close();
}
use of org.hibernate.Session in project hibernate-orm by hibernate.
the class SubselectFetchTest method testSubselectFetchHql.
@Test
public void testSubselectFetchHql() {
Session s = openSession();
Transaction t = s.beginTransaction();
Parent p = new Parent("foo");
p.getChildren().add(new Child("foo1"));
p.getChildren().add(new Child("foo2"));
Parent q = new Parent("bar");
q.getChildren().add(new Child("bar1"));
q.getChildren().add(new Child("bar2"));
q.getMoreChildren().addAll(p.getChildren());
s.persist(p);
s.persist(q);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
sessionFactory().getStatistics().clear();
List parents = s.createQuery("from Parent where name between 'bar' and 'foo' order by name desc").list();
p = (Parent) parents.get(0);
q = (Parent) parents.get(1);
assertFalse(Hibernate.isInitialized(p.getChildren()));
assertFalse(Hibernate.isInitialized(q.getChildren()));
assertEquals(p.getChildren().size(), 2);
assertTrue(Hibernate.isInitialized(p.getChildren().iterator().next()));
assertTrue(Hibernate.isInitialized(q.getChildren()));
assertEquals(q.getChildren().size(), 2);
assertTrue(Hibernate.isInitialized(q.getChildren().iterator().next()));
assertFalse(Hibernate.isInitialized(p.getMoreChildren()));
assertFalse(Hibernate.isInitialized(q.getMoreChildren()));
assertEquals(p.getMoreChildren().size(), 0);
assertTrue(Hibernate.isInitialized(q.getMoreChildren()));
assertEquals(q.getMoreChildren().size(), 2);
assertTrue(Hibernate.isInitialized(q.getMoreChildren().iterator().next()));
assertEquals(3, sessionFactory().getStatistics().getPrepareStatementCount());
Child c = (Child) p.getChildren().get(0);
c.getFriends().size();
s.delete(p);
s.delete(q);
t.commit();
s.close();
}
use of org.hibernate.Session in project hibernate-orm by hibernate.
the class SubselectFetchTest method testManyToManyCriteriaJoin.
@Test
public void testManyToManyCriteriaJoin() {
Session s = openSession();
Transaction t = s.beginTransaction();
Parent p = new Parent("foo");
p.getChildren().add(new Child("foo1"));
p.getChildren().add(new Child("foo2"));
Parent q = new Parent("bar");
q.getChildren().add(new Child("bar1"));
q.getChildren().add(new Child("bar2"));
q.getMoreChildren().addAll(p.getChildren());
s.persist(p);
s.persist(q);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
List parents = s.createCriteria(Parent.class).createCriteria("moreChildren").createCriteria("friends").addOrder(Order.desc("name")).list();
parents = s.createCriteria(Parent.class).setFetchMode("moreChildren", FetchMode.JOIN).setFetchMode("moreChildren.friends", FetchMode.JOIN).addOrder(Order.desc("name")).list();
s.delete(parents.get(0));
s.delete(parents.get(1));
t.commit();
s.close();
}
use of org.hibernate.Session in project hibernate-orm by hibernate.
the class EvictAuditDataAfterCommitTest method testSessionCacheCollectionClear.
@Test
@TestForIssue(jiraKey = "HHH-6614")
public void testSessionCacheCollectionClear() {
final String[] auditEntityNames = new String[] { "org.hibernate.envers.test.entities.onetomany.SetRefEdEntity_AUD", "org.hibernate.envers.test.entities.onetomany.SetRefIngEntity_AUD" };
SetRefEdEntity ed1 = new SetRefEdEntity(1, "data_ed_1");
SetRefEdEntity ed2 = new SetRefEdEntity(2, "data_ed_2");
SetRefIngEntity ing1 = new SetRefIngEntity(3, "data_ing_1");
SetRefIngEntity ing2 = new SetRefIngEntity(4, "data_ing_2");
Session session = openSession();
session.getTransaction().begin();
session.persist(ed1);
session.persist(ed2);
session.persist(ing1);
session.persist(ing2);
session.getTransaction().commit();
checkEmptyAuditSessionCache(session, auditEntityNames);
session.getTransaction().begin();
ed1 = (SetRefEdEntity) session.load(SetRefEdEntity.class, ed1.getId());
ing1.setReference(ed1);
ing2.setReference(ed1);
session.getTransaction().commit();
checkEmptyAuditSessionCache(session, auditEntityNames);
session.getTransaction().begin();
ed2 = (SetRefEdEntity) session.load(SetRefEdEntity.class, ed2.getId());
Set<SetRefIngEntity> reffering = new HashSet<SetRefIngEntity>();
reffering.add(ing1);
reffering.add(ing2);
ed2.setReffering(reffering);
session.getTransaction().commit();
checkEmptyAuditSessionCache(session, auditEntityNames);
session.getTransaction().begin();
ed2 = (SetRefEdEntity) session.load(SetRefEdEntity.class, ed2.getId());
ed2.getReffering().remove(ing1);
session.getTransaction().commit();
checkEmptyAuditSessionCache(session, auditEntityNames);
session.getTransaction().begin();
ed2 = (SetRefEdEntity) session.load(SetRefEdEntity.class, ed2.getId());
ed2.getReffering().iterator().next().setData("mod_data_ing_2");
session.getTransaction().commit();
checkEmptyAuditSessionCache(session, auditEntityNames);
session.close();
}
use of org.hibernate.Session in project head by mifos.
the class LoanRepaymentTag method getMemberScheduleDetails.
@SuppressWarnings("unchecked")
private List<LoanBO> getMemberScheduleDetails(Integer parentId) {
Session hibSession = StaticHibernateUtil.getSessionTL();
Query query = hibSession.getNamedQuery("LoanBO.getMemberDetails");
query.setParameter("ACCOUNT_ID", parentId);
return (List<LoanBO>) query.list();
}
Aggregations