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