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();
}
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();
}
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);
}
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();
}
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();
}
Aggregations