Search in sources :

Example 71 with Session

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();
}
Also used : Transaction(org.hibernate.Transaction) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Example 72 with Session

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();
}
Also used : Transaction(org.hibernate.Transaction) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Example 73 with Session

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();
}
Also used : Transaction(org.hibernate.Transaction) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Example 74 with Session

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();
}
Also used : SetRefIngEntity(org.hibernate.envers.test.entities.onetomany.SetRefIngEntity) SetRefEdEntity(org.hibernate.envers.test.entities.onetomany.SetRefEdEntity) Session(org.hibernate.Session) HashSet(java.util.HashSet) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 75 with Session

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();
}
Also used : Query(org.hibernate.Query) ArrayList(java.util.ArrayList) List(java.util.List) Session(org.hibernate.Session) HttpSession(javax.servlet.http.HttpSession)

Aggregations

Session (org.hibernate.Session)4457 Test (org.junit.Test)2517 Transaction (org.hibernate.Transaction)1327 List (java.util.List)576 ArrayList (java.util.ArrayList)475 Test (org.testng.annotations.Test)411 TestForIssue (org.hibernate.testing.TestForIssue)389 Query (org.hibernate.Query)345 HibernateException (org.hibernate.HibernateException)299 DAOException (com.tomasio.projects.trainning.exception.DAOException)186 Criteria (org.hibernate.Criteria)174 Date (java.util.Date)134 Iterator (java.util.Iterator)127 SkipForDialect (org.hibernate.testing.SkipForDialect)114 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)113 SQLException (java.sql.SQLException)108 Map (java.util.Map)105 SessionFactory (org.hibernate.SessionFactory)105 GradingFactorDaoException (com.remswork.project.alice.dao.exception.GradingFactorDaoException)104 GradingFactorException (com.remswork.project.alice.exception.GradingFactorException)104