use of org.hibernate.criterion.DetachedCriteria in project hibernate-orm by hibernate.
the class CriteriaQueryTest method testMultiplePropertiesSubquery.
@Test
@TestForIssue(jiraKey = "HHH-6766")
public void testMultiplePropertiesSubquery() {
Session session = openSession();
Transaction tx = session.beginTransaction();
Man lukasz = new Man();
lukasz.setName("Lukasz");
lukasz.setHeight(170);
lukasz.setWeight(60);
session.persist(lukasz);
Man robert = new Man();
robert.setName("Robert");
robert.setHeight(170);
robert.setWeight(78);
session.persist(robert);
Woman kinga = new Woman();
kinga.setName("Kinga");
kinga.setHeight(170);
kinga.setWeight(60);
session.persist(kinga);
tx.commit();
session.close();
session = openSession();
tx = session.beginTransaction();
DetachedCriteria sizeQuery = DetachedCriteria.forClass(Man.class).setProjection(Projections.projectionList().add(Projections.property("weight")).add(Projections.property("height"))).add(Restrictions.eq("name", "Lukasz"));
List result;
if (getDialect().supportsRowValueConstructorSyntax()) {
result = session.createCriteria(Woman.class).add(Subqueries.propertiesEq(new String[] { "weight", "height" }, sizeQuery)).list();
assertEquals(1, result.size());
assertEquals(kinga, result.get(0));
}
if (getDialect().supportsRowValueConstructorSyntaxInInList()) {
result = session.createCriteria(Woman.class).add(Subqueries.propertiesIn(new String[] { "weight", "height" }, sizeQuery)).list();
assertEquals(1, result.size());
assertEquals(kinga, result.get(0));
}
tx.commit();
session.close();
session = openSession();
tx = session.beginTransaction();
sizeQuery = DetachedCriteria.forClass(Man.class).setProjection(Projections.projectionList().add(Projections.property("weight")).add(Projections.property("height"))).add(Restrictions.ne("name", "Lukasz"));
if (getDialect().supportsRowValueConstructorSyntax()) {
result = session.createCriteria(Woman.class).add(Subqueries.propertiesNotEq(new String[] { "weight", "height" }, sizeQuery)).list();
assertEquals(1, result.size());
assertEquals(kinga, result.get(0));
}
if (getDialect().supportsRowValueConstructorSyntaxInInList()) {
result = session.createCriteria(Woman.class).add(Subqueries.propertiesNotIn(new String[] { "weight", "height" }, sizeQuery)).list();
assertEquals(1, result.size());
assertEquals(kinga, result.get(0));
}
tx.commit();
session.close();
}
use of org.hibernate.criterion.DetachedCriteria in project hibernate-orm by hibernate.
the class CriteriaQueryTest method testDetachedCriteria.
@Test
public void testDetachedCriteria() {
DetachedCriteria dc = DetachedCriteria.forClass(Student.class).add(Property.forName("name").eq("Gavin King")).addOrder(Order.asc("studentNumber")).setProjection(Property.forName("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);
List result = dc.getExecutableCriteria(session).setMaxResults(3).list();
assertEquals(result.size(), 2);
assertEquals(result.get(0), new Long(232));
assertEquals(result.get(1), new Long(666));
session.delete(gavin);
session.delete(bizarroGavin);
t.commit();
session.close();
}
use of org.hibernate.criterion.DetachedCriteria in project hibernate-orm by hibernate.
the class CriteriaQueryTest method testSubselect.
@Test
@SkipForDialect(value = SybaseASE15Dialect.class, strictMatching = true, jiraKey = "HHH-3032", comment = "I was told this is fixed in Sybase ASE 15.7")
public void testSubselect() {
Session session = openSession();
Transaction t = session.beginTransaction();
Course course = new Course();
course.setCourseCode("HIB");
course.setDescription("Hibernate Training");
session.persist(course);
Student gavin = new Student();
gavin.setName("Gavin King");
gavin.setStudentNumber(232);
session.persist(gavin);
Enrolment enrolment2 = new Enrolment();
enrolment2.setCourse(course);
enrolment2.setCourseCode(course.getCourseCode());
enrolment2.setSemester((short) 3);
enrolment2.setYear((short) 1998);
enrolment2.setStudent(gavin);
enrolment2.setStudentNumber(gavin.getStudentNumber());
gavin.getEnrolments().add(enrolment2);
session.persist(enrolment2);
DetachedCriteria dc = DetachedCriteria.forClass(Student.class).add(Property.forName("studentNumber").eq(new Long(232))).setProjection(Property.forName("name"));
session.createCriteria(Student.class).add(Subqueries.propertyEqAll("name", dc)).list();
session.createCriteria(Student.class).add(Subqueries.exists(dc)).list();
session.createCriteria(Student.class).add(Property.forName("name").eqAll(dc)).list();
session.createCriteria(Student.class).add(Subqueries.in("Gavin King", dc)).list();
DetachedCriteria dc2 = DetachedCriteria.forClass(Student.class, "st").add(Property.forName("st.studentNumber").eqProperty("e.studentNumber")).setProjection(Property.forName("name"));
session.createCriteria(Enrolment.class, "e").add(Subqueries.eq("Gavin King", dc2)).list();
DetachedCriteria dc3 = DetachedCriteria.forClass(Student.class, "st").createCriteria("enrolments").createCriteria("course").add(Property.forName("description").eq("Hibernate Training")).setProjection(Property.forName("st.name"));
session.createCriteria(Enrolment.class, "e").add(Subqueries.eq("Gavin King", dc3)).list();
DetachedCriteria dc4 = DetachedCriteria.forClass(Student.class, "st").setProjection(Property.forName("name").as("stname"));
dc4.getExecutableCriteria(session).list();
// SQL Server and Oracle doesn't normally support ORDER BY in subqueries...
if (!(getDialect() instanceof SQLServerDialect) && !(getDialect() instanceof Oracle8iDialect)) {
dc4.getExecutableCriteria(session).addOrder(Order.asc("stname")).list();
}
session.createCriteria(Enrolment.class, "e").add(Subqueries.eq("Gavin King", dc4)).list();
session.delete(enrolment2);
session.delete(gavin);
session.delete(course);
t.commit();
session.close();
}
use of org.hibernate.criterion.DetachedCriteria in project hibernate-orm by hibernate.
the class DynamicFilterTest method testCriteriaControl.
@Test
public void testCriteriaControl() {
TestData testData = new TestData();
testData.prepare();
// the subquery...
DetachedCriteria subquery = DetachedCriteria.forClass(Salesperson.class).setProjection(Property.forName("name"));
Session session = openSession();
session.beginTransaction();
session.enableFilter("fulfilledOrders").setParameter("asOfDate", testData.lastMonth.getTime());
session.enableFilter("regionlist").setParameterList("regions", new String[] { "APAC" });
List result = session.createCriteria(Order.class).add(Subqueries.in("steve", subquery)).list();
assertEquals(1, result.size());
session.getTransaction().commit();
session.close();
testData.release();
}
use of org.hibernate.criterion.DetachedCriteria in project hibernate-orm by hibernate.
the class CriteriaQueryWithAppliedFilterTest method testSubquery.
@Test
public void testSubquery() {
doInHibernate(this::sessionFactory, session -> {
final Criteria query = session.createCriteria(Student.class);
query.add(Restrictions.eq("name", "dre"));
final DetachedCriteria inner = DetachedCriteria.forClass(Student.class);
inner.setProjection(Projections.min("age"));
query.add(Property.forName("age").eq(inner));
query.add(Restrictions.eq("name", "dre"));
final List list = query.list();
assertThat(list.size(), is(1));
});
doInHibernate(this::sessionFactory, session -> {
session.enableFilter("statusFilter").setParameter("status", "deleted");
final Criteria query = session.createCriteria(Student.class);
query.add(Restrictions.eq("name", "dre"));
final DetachedCriteria inner = DetachedCriteria.forClass(Student.class);
inner.setProjection(Projections.min("age"));
query.add(Property.forName("age").eq(inner));
query.add(Restrictions.eq("name", "dre"));
final List list = query.list();
assertThat(list.size(), is(0));
});
}
Aggregations