Search in sources :

Example 31 with Criteria

use of org.hibernate.Criteria in project hibernate-orm by hibernate.

the class OrderedManyToManyTest method testManyToManyOrdering.

@Test
public void testManyToManyOrdering() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    User gavin = new User("gavin", "jboss");
    User steve = new User("steve", "jboss");
    User max = new User("max", "jboss");
    User emmanuel = new User("emmanuel", "jboss");
    s.persist(gavin);
    s.persist(steve);
    s.persist(max);
    s.persist(emmanuel);
    Group hibernate = new Group("hibernate", "jboss");
    hibernate.addUser(gavin);
    hibernate.addUser(steve);
    hibernate.addUser(max);
    hibernate.addUser(emmanuel);
    s.persist(hibernate);
    t.commit();
    s.close();
    // delayed collection load...
    s = openSession();
    t = s.beginTransaction();
    hibernate = (Group) s.get(Group.class, hibernate.getId());
    assertFalse(Hibernate.isInitialized(hibernate.getUsers()));
    assertEquals(4, hibernate.getUsers().size());
    assertOrdering(hibernate.getUsers());
    t.commit();
    s.close();
    // HQL (non eager)
    s = openSession();
    t = s.beginTransaction();
    hibernate = (Group) s.createQuery("from Group").uniqueResult();
    assertFalse(Hibernate.isInitialized(hibernate.getUsers()));
    assertEquals(4, hibernate.getUsers().size());
    assertOrdering(hibernate.getUsers());
    t.commit();
    s.close();
    // HQL (eager)
    s = openSession();
    t = s.beginTransaction();
    hibernate = (Group) s.createQuery("from Group g inner join fetch g.users").uniqueResult();
    assertTrue(Hibernate.isInitialized(hibernate.getUsers()));
    assertEquals(4, hibernate.getUsers().size());
    assertOrdering(hibernate.getUsers());
    t.commit();
    s.close();
    // criteria load (forced eager fetch)
    s = openSession();
    t = s.beginTransaction();
    Criteria criteria = s.createCriteria(Group.class);
    criteria.setFetchMode("users", FetchMode.JOIN);
    hibernate = (Group) criteria.uniqueResult();
    assertTrue(Hibernate.isInitialized(hibernate.getUsers()));
    assertEquals(4, hibernate.getUsers().size());
    assertOrdering(hibernate.getUsers());
    t.commit();
    s.close();
    // criteria load (forced non eager fetch)
    s = openSession();
    t = s.beginTransaction();
    criteria = s.createCriteria(Group.class);
    criteria.setFetchMode("users", FetchMode.SELECT);
    hibernate = (Group) criteria.uniqueResult();
    assertFalse(Hibernate.isInitialized(hibernate.getUsers()));
    assertEquals(4, hibernate.getUsers().size());
    assertOrdering(hibernate.getUsers());
    t.commit();
    s.close();
    // clean up
    s = openSession();
    t = s.beginTransaction();
    s.delete(gavin);
    s.delete(steve);
    s.delete(max);
    s.delete(emmanuel);
    s.delete(hibernate);
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Criteria(org.hibernate.Criteria) Session(org.hibernate.Session) Test(org.junit.Test)

Example 32 with Criteria

use of org.hibernate.Criteria in project hibernate-orm by hibernate.

the class CachedPropertyRefCollectionTest method testRetrievalOfCachedCollectionWithPropertyRefKey.

@Test
public void testRetrievalOfCachedCollectionWithPropertyRefKey() {
    // create the test data...
    Session session = openSession();
    session.beginTransaction();
    ManagedObject mo = new ManagedObject("test", "test");
    mo.getMembers().add("members");
    session.save(mo);
    session.getTransaction().commit();
    session.close();
    // First attempt to load it via PK lookup
    session = openSession();
    session.beginTransaction();
    ManagedObject obj = (ManagedObject) session.get(ManagedObject.class, 1L);
    assertNotNull(obj);
    assertTrue(Hibernate.isInitialized(obj));
    obj.getMembers().size();
    assertTrue(Hibernate.isInitialized(obj.getMembers()));
    session.getTransaction().commit();
    session.close();
    // Now try to access it via natural key
    session = openSession();
    session.beginTransaction();
    Criteria criteria = session.createCriteria(ManagedObject.class).add(Restrictions.naturalId().set("name", "test")).setCacheable(true).setFetchMode("members", FetchMode.JOIN);
    obj = (ManagedObject) criteria.uniqueResult();
    assertNotNull(obj);
    assertTrue(Hibernate.isInitialized(obj));
    obj.getMembers().size();
    assertTrue(Hibernate.isInitialized(obj.getMembers()));
    session.getTransaction().commit();
    session.close();
    // Clean up
    session = openSession();
    session.beginTransaction();
    session.delete(obj);
    session.getTransaction().commit();
    session.close();
}
Also used : Criteria(org.hibernate.Criteria) Session(org.hibernate.Session) Test(org.junit.Test)

Example 33 with Criteria

use of org.hibernate.Criteria in project hibernate-orm by hibernate.

the class QueryCacheTest method testGetByCompositeId.

@Test
@TestForIssue(jiraKey = "HHH-4459")
public void testGetByCompositeId() {
    Session s = openSession();
    s.beginTransaction();
    s.persist(new EntityWithCompositeKey(PK));
    Query query = s.createQuery("FROM EntityWithCompositeKey e WHERE e.pk = :pk");
    query.setCacheable(true);
    query.setParameter("pk", PK);
    assertEquals(1, query.list().size());
    s.getTransaction().rollback();
    s.close();
    s = openSession();
    s.beginTransaction();
    EntityWithStringCompositeKey entity = new EntityWithStringCompositeKey();
    StringCompositeKey key = new StringCompositeKey();
    key.setAnalog("foo1");
    key.setDevice("foo2");
    key.setDeviceType("foo3");
    key.setSubstation("foo4");
    entity.setPk(key);
    s.persist(entity);
    Criteria c = s.createCriteria(EntityWithStringCompositeKey.class).add(Restrictions.eq("pk", key));
    c.setCacheable(true);
    assertEquals(1, c.list().size());
    s.getTransaction().rollback();
    s.close();
}
Also used : SQLQuery(org.hibernate.SQLQuery) Query(org.hibernate.Query) Criteria(org.hibernate.Criteria) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 34 with Criteria

use of org.hibernate.Criteria in project hibernate-orm by hibernate.

the class ReadOnlyCriteriaQueryTest method testModifiableSessionReadOnlyCriteria.

@Test
public void testModifiableSessionReadOnlyCriteria() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Course course = new Course();
    course.setCourseCode("HIB");
    course.setDescription("Hibernate Training");
    s.persist(course);
    Course coursePreferred = new Course();
    coursePreferred.setCourseCode("JBOSS");
    coursePreferred.setDescription("JBoss");
    s.persist(coursePreferred);
    Student gavin = new Student();
    gavin.setName("Gavin King");
    gavin.setStudentNumber(232);
    gavin.setPreferredCourse(coursePreferred);
    s.persist(gavin);
    Enrolment enrolment = new Enrolment();
    enrolment.setCourse(course);
    enrolment.setCourseCode(course.getCourseCode());
    enrolment.setSemester((short) 3);
    enrolment.setYear((short) 1998);
    enrolment.setStudent(gavin);
    enrolment.setStudentNumber(gavin.getStudentNumber());
    gavin.getEnrolments().add(enrolment);
    s.persist(enrolment);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    Criteria criteria = s.createCriteria(Student.class).setReadOnly(true);
    assertFalse(s.isDefaultReadOnly());
    assertTrue(criteria.isReadOnlyInitialized());
    assertTrue(criteria.isReadOnly());
    gavin = (Student) criteria.uniqueResult();
    assertFalse(s.isDefaultReadOnly());
    assertTrue(criteria.isReadOnlyInitialized());
    assertTrue(criteria.isReadOnly());
    assertTrue(s.isReadOnly(gavin));
    assertFalse(Hibernate.isInitialized(gavin.getPreferredCourse()));
    checkProxyReadOnly(s, gavin.getPreferredCourse(), true);
    assertFalse(Hibernate.isInitialized(gavin.getPreferredCourse()));
    Hibernate.initialize(gavin.getPreferredCourse());
    assertTrue(Hibernate.isInitialized(gavin.getPreferredCourse()));
    checkProxyReadOnly(s, gavin.getPreferredCourse(), true);
    assertFalse(Hibernate.isInitialized(gavin.getEnrolments()));
    Hibernate.initialize(gavin.getEnrolments());
    assertTrue(Hibernate.isInitialized(gavin.getEnrolments()));
    assertEquals(1, gavin.getEnrolments().size());
    enrolment = (Enrolment) gavin.getEnrolments().iterator().next();
    assertFalse(s.isReadOnly(enrolment));
    assertFalse(Hibernate.isInitialized(enrolment.getCourse()));
    checkProxyReadOnly(s, enrolment.getCourse(), false);
    Hibernate.initialize(enrolment.getCourse());
    checkProxyReadOnly(s, enrolment.getCourse(), false);
    s.delete(gavin.getPreferredCourse());
    s.delete(gavin);
    s.delete(enrolment.getCourse());
    s.delete(enrolment);
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Criteria(org.hibernate.Criteria) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) Session(org.hibernate.Session) Test(org.junit.Test)

Example 35 with Criteria

use of org.hibernate.Criteria in project hibernate-orm by hibernate.

the class ReadOnlyCriteriaQueryTest method testReadOnlyCriteriaReturnsExistingModifiableProxyNotInit.

@Test
public void testReadOnlyCriteriaReturnsExistingModifiableProxyNotInit() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Course course = new Course();
    course.setCourseCode("HIB");
    course.setDescription("Hibernate Training");
    s.persist(course);
    Course coursePreferred = new Course();
    coursePreferred.setCourseCode("JBOSS");
    coursePreferred.setDescription("JBoss");
    s.persist(coursePreferred);
    Student gavin = new Student();
    gavin.setName("Gavin King");
    gavin.setStudentNumber(232);
    gavin.setPreferredCourse(coursePreferred);
    s.persist(gavin);
    Enrolment enrolment = new Enrolment();
    enrolment.setCourse(course);
    enrolment.setCourseCode(course.getCourseCode());
    enrolment.setSemester((short) 3);
    enrolment.setYear((short) 1998);
    enrolment.setStudent(gavin);
    enrolment.setStudentNumber(gavin.getStudentNumber());
    gavin.getEnrolments().add(enrolment);
    s.persist(enrolment);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    assertFalse(s.isDefaultReadOnly());
    coursePreferred = (Course) s.load(Course.class, coursePreferred.getCourseCode());
    assertFalse(Hibernate.isInitialized(coursePreferred));
    checkProxyReadOnly(s, coursePreferred, false);
    Criteria criteria = s.createCriteria(Student.class).setReadOnly(true);
    assertTrue(criteria.isReadOnlyInitialized());
    assertTrue(criteria.isReadOnly());
    gavin = (Student) criteria.uniqueResult();
    assertFalse(s.isDefaultReadOnly());
    assertTrue(criteria.isReadOnlyInitialized());
    assertTrue(criteria.isReadOnly());
    assertTrue(s.isReadOnly(gavin));
    assertFalse(Hibernate.isInitialized(coursePreferred));
    checkProxyReadOnly(s, coursePreferred, false);
    Hibernate.initialize(coursePreferred);
    checkProxyReadOnly(s, coursePreferred, false);
    assertFalse(Hibernate.isInitialized(gavin.getEnrolments()));
    Hibernate.initialize(gavin.getEnrolments());
    assertTrue(Hibernate.isInitialized(gavin.getEnrolments()));
    assertEquals(1, gavin.getEnrolments().size());
    enrolment = (Enrolment) gavin.getEnrolments().iterator().next();
    assertFalse(s.isReadOnly(enrolment));
    assertFalse(Hibernate.isInitialized(enrolment.getCourse()));
    checkProxyReadOnly(s, enrolment.getCourse(), false);
    Hibernate.initialize(enrolment.getCourse());
    checkProxyReadOnly(s, enrolment.getCourse(), false);
    s.delete(gavin.getPreferredCourse());
    s.delete(gavin);
    s.delete(enrolment.getCourse());
    s.delete(enrolment);
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Criteria(org.hibernate.Criteria) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

Criteria (org.hibernate.Criteria)180 Session (org.hibernate.Session)95 Test (org.junit.Test)69 List (java.util.List)39 Transaction (org.hibernate.Transaction)39 DetachedCriteria (org.hibernate.criterion.DetachedCriteria)27 ArrayList (java.util.ArrayList)15 TestForIssue (org.hibernate.testing.TestForIssue)12 Iterator (java.util.Iterator)9 Period (org.hisp.dhis.period.Period)8 Map (java.util.Map)6 State (org.hibernate.test.cache.infinispan.functional.entities.State)5 OnmsCriteria (org.opennms.netmgt.model.OnmsCriteria)5 HibernateCallback (org.springframework.orm.hibernate3.HibernateCallback)5 HashSet (java.util.HashSet)3 Criterion (org.hibernate.criterion.Criterion)3 Statistics (org.hibernate.stat.Statistics)3 Pager (org.hisp.dhis.common.Pager)3 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)2 HashMap (java.util.HashMap)2