use of org.hibernate.criterion.DetachedCriteria in project hibernate-orm by hibernate.
the class CriteriaQueryWithAppliedFilterTest method testSubqueryWithRestrictionsOnComponentTypes.
@Test
public void testSubqueryWithRestrictionsOnComponentTypes() {
doInHibernate(this::sessionFactory, session -> {
session.enableFilter("statusFilter").setParameter("status", "active");
final Criteria query = session.createCriteria(Student.class);
query.add(Restrictions.eq("id", STUDENT_ID));
final DetachedCriteria subSelect = DetachedCriteria.forClass(Student.class);
subSelect.setProjection(Projections.max("age"));
subSelect.add(Restrictions.eq("id", STUDENT_ID));
query.add(Property.forName("age").eq(subSelect));
final List list = query.list();
assertThat(list.size(), is(1));
});
}
use of org.hibernate.criterion.DetachedCriteria in project hibernate-orm by hibernate.
the class DynamicFilterTest method testCriteriaSubqueryWithFilters.
@Test
public void testCriteriaSubqueryWithFilters() {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Criteria-subquery test
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
log.info("Starting Criteria-subquery filter tests");
TestData testData = new TestData();
testData.prepare();
Session session = openSession();
session.enableFilter("region").setParameter("region", "APAC");
log.info("Criteria query against Department with a subquery on Salesperson in the APAC reqion...");
DetachedCriteria salespersonSubquery = DetachedCriteria.forClass(Salesperson.class).add(Restrictions.eq("name", "steve")).setProjection(Property.forName("department"));
Criteria departmentsQuery = session.createCriteria(Department.class).add(Subqueries.propertyIn("id", salespersonSubquery));
List departments = departmentsQuery.list();
assertEquals("Incorrect department count", 1, departments.size());
log.info("Criteria query against Department with a subquery on Salesperson in the FooBar reqion...");
session.enableFilter("region").setParameter("region", "Foobar");
departments = departmentsQuery.list();
assertEquals("Incorrect department count", 0, departments.size());
log.info("Criteria query against Order with a subquery for line items with a subquery on product and sold by a given sales person...");
session.enableFilter("region").setParameter("region", "APAC");
DetachedCriteria lineItemSubquery = DetachedCriteria.forClass(LineItem.class).add(Restrictions.ge("quantity", 1L)).createCriteria("product").add(Restrictions.eq("name", "Acme Hair Gel")).setProjection(Property.forName("id"));
List orders = session.createCriteria(Order.class).add(Subqueries.exists(lineItemSubquery)).add(Restrictions.eq("buyer", "gavin")).list();
assertEquals("Incorrect orders count", 1, orders.size());
log.info("query against Order with a subquery for line items with a subquery line items where the product name is Acme Hair Gel and the quantity is greater than 1 in a given region and the product is effective as of last month");
session.enableFilter("region").setParameter("region", "APAC");
session.enableFilter("effectiveDate").setParameter("asOfDate", testData.lastMonth.getTime());
DetachedCriteria productSubquery = DetachedCriteria.forClass(Product.class).add(Restrictions.eq("name", "Acme Hair Gel")).setProjection(Property.forName("id"));
lineItemSubquery = DetachedCriteria.forClass(LineItem.class).add(Restrictions.ge("quantity", 1L)).createCriteria("product").add(Subqueries.propertyIn("id", productSubquery)).setProjection(Property.forName("id"));
orders = session.createCriteria(Order.class).add(Subqueries.exists(lineItemSubquery)).add(Restrictions.eq("buyer", "gavin")).list();
assertEquals("Incorrect orders count", 1, orders.size());
log.info("query against Order with a subquery for line items with a subquery line items where the product name is Acme Hair Gel and the quantity is greater than 1 in a given region and the product is effective as of 4 months ago");
session.enableFilter("region").setParameter("region", "APAC");
session.enableFilter("effectiveDate").setParameter("asOfDate", testData.fourMonthsAgo.getTime());
orders = session.createCriteria(Order.class).add(Subqueries.exists(lineItemSubquery)).add(Restrictions.eq("buyer", "gavin")).list();
assertEquals("Incorrect orders count", 0, orders.size());
session.close();
testData.release();
}
use of org.hibernate.criterion.DetachedCriteria in project hibernate-orm by hibernate.
the class ReadOnlyCriteriaQueryTest method testDetachedCriteria.
@Test
public void testDetachedCriteria() {
DetachedCriteria dc = DetachedCriteria.forClass(Student.class).add(Property.forName("name").eq("Gavin King")).addOrder(Order.asc("studentNumber"));
byte[] bytes = SerializationHelper.serialize(dc);
dc = (DetachedCriteria) SerializationHelper.deserialize(bytes);
Session session = openSession();
Transaction t = session.beginTransaction();
Student gavin = new Student();
gavin.setName("Gavin King");
gavin.setStudentNumber(232);
Student bizarroGavin = new Student();
bizarroGavin.setName("Gavin King");
bizarroGavin.setStudentNumber(666);
session.persist(bizarroGavin);
session.persist(gavin);
t.commit();
session.close();
session = openSession();
t = session.beginTransaction();
List result = dc.getExecutableCriteria(session).setMaxResults(3).setReadOnly(true).list();
assertEquals(result.size(), 2);
gavin = (Student) result.get(0);
bizarroGavin = (Student) result.get(1);
assertEquals(232, gavin.getStudentNumber());
assertEquals(666, bizarroGavin.getStudentNumber());
assertTrue(session.isReadOnly(gavin));
assertTrue(session.isReadOnly(bizarroGavin));
session.delete(gavin);
session.delete(bizarroGavin);
t.commit();
session.close();
}
use of org.hibernate.criterion.DetachedCriteria in project hibernate-orm by hibernate.
the class ReadOnlyCriteriaQueryTest method testSubselect.
@Test
@SkipForDialect(value = SybaseASE15Dialect.class, jiraKey = "HHH-3032", strictMatching = true)
public void testSubselect() {
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();
DetachedCriteria dc = DetachedCriteria.forClass(Student.class).add(Property.forName("studentNumber").eq(new Long(232))).setProjection(Property.forName("name"));
gavin = (Student) s.createCriteria(Student.class).add(Subqueries.exists(dc)).setReadOnly(true).uniqueResult();
assertFalse(s.isDefaultReadOnly());
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);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
DetachedCriteria dc2 = DetachedCriteria.forClass(Student.class, "st").add(Property.forName("st.studentNumber").eqProperty("e.studentNumber")).setProjection(Property.forName("name"));
enrolment = (Enrolment) s.createCriteria(Enrolment.class, "e").add(Subqueries.eq("Gavin King", dc2)).setReadOnly(true).uniqueResult();
assertTrue(s.isReadOnly(enrolment));
assertFalse(Hibernate.isInitialized(enrolment.getCourse()));
checkProxyReadOnly(s, enrolment.getCourse(), true);
Hibernate.initialize(enrolment.getCourse());
assertTrue(Hibernate.isInitialized(enrolment.getCourse()));
checkProxyReadOnly(s, enrolment.getCourse(), true);
assertFalse(Hibernate.isInitialized(enrolment.getStudent()));
checkProxyReadOnly(s, enrolment.getStudent(), true);
Hibernate.initialize(enrolment.getStudent());
assertTrue(Hibernate.isInitialized(enrolment.getStudent()));
checkProxyReadOnly(s, enrolment.getStudent(), true);
assertFalse(Hibernate.isInitialized(enrolment.getStudent().getPreferredCourse()));
checkProxyReadOnly(s, enrolment.getStudent().getPreferredCourse(), false);
Hibernate.initialize(enrolment.getStudent().getPreferredCourse());
assertTrue(Hibernate.isInitialized(enrolment.getStudent().getPreferredCourse()));
checkProxyReadOnly(s, enrolment.getStudent().getPreferredCourse(), false);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
DetachedCriteria dc3 = DetachedCriteria.forClass(Student.class, "st").createCriteria("enrolments").createCriteria("course").add(Property.forName("description").eq("Hibernate Training")).setProjection(Property.forName("st.name"));
enrolment = (Enrolment) s.createCriteria(Enrolment.class, "e").add(Subqueries.eq("Gavin King", dc3)).setReadOnly(true).uniqueResult();
assertTrue(s.isReadOnly(enrolment));
assertFalse(Hibernate.isInitialized(enrolment.getCourse()));
checkProxyReadOnly(s, enrolment.getCourse(), true);
Hibernate.initialize(enrolment.getCourse());
assertTrue(Hibernate.isInitialized(enrolment.getCourse()));
checkProxyReadOnly(s, enrolment.getCourse(), true);
assertFalse(Hibernate.isInitialized(enrolment.getStudent()));
checkProxyReadOnly(s, enrolment.getStudent(), true);
Hibernate.initialize(enrolment.getStudent());
assertTrue(Hibernate.isInitialized(enrolment.getStudent()));
checkProxyReadOnly(s, enrolment.getStudent(), true);
assertFalse(Hibernate.isInitialized(enrolment.getStudent().getPreferredCourse()));
checkProxyReadOnly(s, enrolment.getStudent().getPreferredCourse(), false);
Hibernate.initialize(enrolment.getStudent().getPreferredCourse());
assertTrue(Hibernate.isInitialized(enrolment.getStudent().getPreferredCourse()));
checkProxyReadOnly(s, enrolment.getStudent().getPreferredCourse(), false);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
s.delete(gavin.getPreferredCourse());
s.delete(gavin);
enrolment = (Enrolment) gavin.getEnrolments().iterator().next();
s.delete(enrolment.getCourse());
s.delete(enrolment);
t.commit();
s.close();
}
use of org.hibernate.criterion.DetachedCriteria in project dhis2-core by dhis2.
the class HibernateGenericStore method getSharingDetachedCriteria.
private DetachedCriteria getSharingDetachedCriteria(UserInfo user, String access) {
DetachedCriteria criteria = DetachedCriteria.forClass(getClazz(), "c");
preProcessDetachedCriteria(criteria);
if (!sharingEnabled(user) || user == null) {
return criteria;
}
Assert.notNull(user, "User argument can't be null.");
Disjunction disjunction = Restrictions.disjunction();
disjunction.add(Restrictions.like("c.publicAccess", access));
disjunction.add(Restrictions.isNull("c.publicAccess"));
disjunction.add(Restrictions.isNull("c.user.id"));
disjunction.add(Restrictions.eq("c.user.id", user.getId()));
DetachedCriteria userGroupDetachedCriteria = DetachedCriteria.forClass(getClazz(), "ugdc");
userGroupDetachedCriteria.createCriteria("ugdc.userGroupAccesses", "uga");
userGroupDetachedCriteria.createCriteria("uga.userGroup", "ug");
userGroupDetachedCriteria.createCriteria("ug.members", "ugm");
userGroupDetachedCriteria.add(Restrictions.eqProperty("ugdc.id", "c.id"));
userGroupDetachedCriteria.add(Restrictions.eq("ugm.id", user.getId()));
userGroupDetachedCriteria.add(Restrictions.like("uga.access", access));
userGroupDetachedCriteria.setProjection(Property.forName("uga.id"));
disjunction.add(Subqueries.exists(userGroupDetachedCriteria));
DetachedCriteria userDetachedCriteria = DetachedCriteria.forClass(getClazz(), "udc");
userDetachedCriteria.createCriteria("udc.userAccesses", "ua");
userDetachedCriteria.createCriteria("ua.user", "u");
userDetachedCriteria.add(Restrictions.eqProperty("udc.id", "c.id"));
userDetachedCriteria.add(Restrictions.eq("u.id", user.getId()));
userDetachedCriteria.add(Restrictions.like("ua.access", access));
userDetachedCriteria.setProjection(Property.forName("ua.id"));
disjunction.add(Subqueries.exists(userDetachedCriteria));
criteria.add(disjunction);
return criteria;
}
Aggregations