Search in sources :

Example 36 with Criteria

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

the class ReadOnlyCriteriaQueryTest method testReadOnlyCriteriaReturnsExistingModifiableProxyInit.

@Test
public void testReadOnlyCriteriaReturnsExistingModifiableProxyInit() {
    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);
    Hibernate.initialize(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));
    assertTrue(Hibernate.isInitialized(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)

Example 37 with Criteria

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

the class ReadOnlyCriteriaQueryTest method testModifiableSessionModifiableCriteria.

@Test
public void testModifiableSessionModifiableCriteria() {
    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);
    assertFalse(s.isDefaultReadOnly());
    assertFalse(criteria.isReadOnlyInitialized());
    assertFalse(criteria.isReadOnly());
    criteria.setReadOnly(false);
    assertTrue(criteria.isReadOnlyInitialized());
    assertFalse(criteria.isReadOnly());
    gavin = (Student) criteria.uniqueResult();
    assertFalse(s.isDefaultReadOnly());
    assertTrue(criteria.isReadOnlyInitialized());
    assertFalse(criteria.isReadOnly());
    assertFalse(s.isReadOnly(gavin));
    assertFalse(Hibernate.isInitialized(gavin.getPreferredCourse()));
    checkProxyReadOnly(s, gavin.getPreferredCourse(), false);
    assertFalse(Hibernate.isInitialized(gavin.getPreferredCourse()));
    Hibernate.initialize(gavin.getPreferredCourse());
    assertTrue(Hibernate.isInitialized(gavin.getPreferredCourse()));
    checkProxyReadOnly(s, gavin.getPreferredCourse(), 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)

Example 38 with Criteria

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

the class ReadOnlyCriteriaQueryTest method testModifiableSessionDefaultCriteria.

@Test
public void testModifiableSessionDefaultCriteria() {
    clearCounts();
    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();
    assertInsertCount(4);
    assertUpdateCount(0);
    clearCounts();
    s = openSession();
    t = s.beginTransaction();
    Criteria criteria = s.createCriteria(Student.class);
    assertFalse(s.isDefaultReadOnly());
    assertFalse(criteria.isReadOnlyInitialized());
    assertFalse(criteria.isReadOnly());
    gavin = (Student) criteria.uniqueResult();
    assertFalse(s.isDefaultReadOnly());
    assertFalse(criteria.isReadOnlyInitialized());
    assertFalse(criteria.isReadOnly());
    assertFalse(s.isReadOnly(gavin));
    assertFalse(Hibernate.isInitialized(gavin.getPreferredCourse()));
    checkProxyReadOnly(s, gavin.getPreferredCourse(), false);
    assertFalse(Hibernate.isInitialized(gavin.getPreferredCourse()));
    Hibernate.initialize(gavin.getPreferredCourse());
    assertTrue(Hibernate.isInitialized(gavin.getPreferredCourse()));
    checkProxyReadOnly(s, gavin.getPreferredCourse(), 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();
    assertUpdateCount(1);
    assertDeleteCount(4);
}
Also used : Transaction(org.hibernate.Transaction) Criteria(org.hibernate.Criteria) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) Session(org.hibernate.Session) Test(org.junit.Test)

Example 39 with Criteria

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

the class ReadOnlyCriteriaQueryTest method testReadOnlySessionDefaultCriteria.

@Test
public void testReadOnlySessionDefaultCriteria() {
    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();
    s.setDefaultReadOnly(true);
    Criteria criteria = s.createCriteria(Student.class);
    assertTrue(s.isDefaultReadOnly());
    assertFalse(criteria.isReadOnlyInitialized());
    assertTrue(criteria.isReadOnly());
    gavin = (Student) criteria.uniqueResult();
    assertTrue(s.isDefaultReadOnly());
    assertFalse(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();
    assertTrue(s.isReadOnly(enrolment));
    assertFalse(Hibernate.isInitialized(enrolment.getCourse()));
    checkProxyReadOnly(s, enrolment.getCourse(), true);
    Hibernate.initialize(enrolment.getCourse());
    checkProxyReadOnly(s, enrolment.getCourse(), true);
    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 40 with Criteria

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

the class ReadOnlyCriteriaQueryTest method testReadOnlySessionModifiableCriteria.

@Test
public void testReadOnlySessionModifiableCriteria() {
    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();
    s.setDefaultReadOnly(true);
    Criteria criteria = s.createCriteria(Student.class);
    assertTrue(s.isDefaultReadOnly());
    assertFalse(criteria.isReadOnlyInitialized());
    assertTrue(criteria.isReadOnly());
    criteria.setReadOnly(false);
    assertTrue(criteria.isReadOnlyInitialized());
    assertFalse(criteria.isReadOnly());
    gavin = (Student) criteria.uniqueResult();
    assertTrue(s.isDefaultReadOnly());
    assertTrue(criteria.isReadOnlyInitialized());
    assertFalse(criteria.isReadOnly());
    assertFalse(s.isReadOnly(gavin));
    assertFalse(Hibernate.isInitialized(gavin.getPreferredCourse()));
    checkProxyReadOnly(s, gavin.getPreferredCourse(), false);
    assertFalse(Hibernate.isInitialized(gavin.getPreferredCourse()));
    Hibernate.initialize(gavin.getPreferredCourse());
    assertTrue(Hibernate.isInitialized(gavin.getPreferredCourse()));
    checkProxyReadOnly(s, gavin.getPreferredCourse(), 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();
    assertTrue(s.isReadOnly(enrolment));
    assertFalse(Hibernate.isInitialized(enrolment.getCourse()));
    checkProxyReadOnly(s, enrolment.getCourse(), true);
    Hibernate.initialize(enrolment.getCourse());
    checkProxyReadOnly(s, enrolment.getCourse(), true);
    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