List of usage examples for org.hibernate.criterion Restrictions conjunction
public static Conjunction conjunction()
From source file:com.evolveum.midpoint.repo.sql.query.restriction.ReferenceRestriction.java
License:Apache License
@Override public Criterion interpretInternal(RefFilter filter) throws QueryException { List<? extends PrismValue> values = filter.getValues(); PrismReferenceValue refValue = null; if (values != null && !values.isEmpty()) { refValue = (PrismReferenceValue) values.get(0); }/*from w w w. j ava2 s . c o m*/ if (refValue == null) { throw new QueryException("Ref filter '" + filter + "' doesn't contain reference value."); } String prefix = createPropertyNamePrefix(filter); Conjunction conjunction = Restrictions.conjunction(); conjunction.add(handleEqOrNull(prefix + ObjectReference.F_TARGET_OID, refValue.getOid())); if (refValue.getRelation() != null) { QName relation = refValue.getRelation(); conjunction.add(handleEqOrNull(prefix + ObjectReference.F_RELATION, RUtil.qnameToString(relation))); } if (refValue.getTargetType() != null) { conjunction.add(handleEqOrNull(prefix + ObjectReference.F_TYPE, ClassMapper.getHQLTypeForQName(refValue.getTargetType()))); } return conjunction; }
From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java
License:Apache License
@Test public void queryOrganizationStrict() throws Exception { Session session = open();//from w ww .j av a2s. com try { ObjectFilter filter = EqualFilter.createEqual(UserType.F_ORGANIZATION, UserType.class, prismContext, null, new PolyString("asdf", "asdf")); ObjectQuery query = ObjectQuery.createObjectQuery(filter); Criteria main = session.createCriteria(RUser.class, "u"); ProjectionList projections = Projections.projectionList(); addFullObjectProjectionList("u", projections, false); main.setProjection(projections); Criteria o = main.createCriteria("organization", "o", JoinType.LEFT_OUTER_JOIN); o.add(Restrictions.conjunction().add(Restrictions.eq("o.orig", "asdf")) .add(Restrictions.eq("o.norm", "asdf"))); String expected = HibernateToSqlTranslator.toSql(main); String real = getInterpretedQuery(session, UserType.class, query); LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[] { expected, real }); AssertJUnit.assertEquals(expected, real); } finally { close(session); } }
From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java
License:Apache License
@Test public void queryGenericLong() throws Exception { Session session = open();/*from ww w . j av a 2 s .co m*/ try { Criteria main = session.createCriteria(RGenericObject.class, "g"); Criteria stringExt = main.createCriteria("longs", "l", JoinType.LEFT_OUTER_JOIN); //and Criterion c1 = Restrictions.eq("name.norm", "generic object"); //and Conjunction c2 = Restrictions.conjunction(); c2.add(Restrictions.eq("l.ownerType", RObjectExtensionType.EXTENSION)); c2.add(Restrictions.eq("l.name", new QName("http://example.com/p", "intType"))); c2.add(Restrictions.eq("l.value", 123L)); Conjunction conjunction = Restrictions.conjunction(); conjunction.add(c1); conjunction.add(c2); main.add(conjunction); ProjectionList projections = Projections.projectionList(); addFullObjectProjectionList("g", projections, false); main.setProjection(projections); String expected = HibernateToSqlTranslator.toSql(main); String real = getInterpretedQuery(session, GenericObjectType.class, new File(TEST_DIR, "query-and-generic.xml")); LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[] { expected, real }); AssertJUnit.assertEquals(expected, real); } finally { close(session); } }
From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java
License:Apache License
@Test public void queryOrComposite() throws Exception { Session session = open();// w ww.j a va 2 s . c om try { Criteria main = session.createCriteria(RShadow.class, "r"); ProjectionList projections = Projections.projectionList(); addFullObjectProjectionList("r", projections, false); main.setProjection(projections); Criteria stringExt = main.createCriteria("strings", "s1", JoinType.LEFT_OUTER_JOIN); //or Criterion c1 = Restrictions.eq("intent", "some account type"); //or Conjunction c2 = Restrictions.conjunction(); c2.add(Restrictions.eq("s1.ownerType", RObjectExtensionType.ATTRIBUTES)); c2.add(Restrictions.eq("s1.name", new QName("http://midpoint.evolveum.com/blabla", "foo"))); c2.add(Restrictions.eq("s1.value", "foo value")); //or Conjunction c3 = Restrictions.conjunction(); c3.add(Restrictions.eq("s1.ownerType", RObjectExtensionType.EXTENSION)); c3.add(Restrictions.eq("s1.name", new QName("http://example.com/p", "stringType"))); c3.add(Restrictions.eq("s1.value", "uid=test,dc=example,dc=com")); //or Conjunction c4 = Restrictions.conjunction(); c4.add(Restrictions.eq("r.resourceRef.targetOid", "d0db5be9-cb93-401f-b6c1-86ffffe4cd5e")); c4.add(Restrictions.eq("r.resourceRef.type", QNameUtil.qNameToUri(ResourceType.COMPLEX_TYPE))); Disjunction disjunction = Restrictions.disjunction(); disjunction.add(c1); disjunction.add(c2); disjunction.add(c3); disjunction.add(c4); main.add(disjunction); String expected = HibernateToSqlTranslator.toSql(main); String real = getInterpretedQuery(session, ShadowType.class, new File(TEST_DIR, "query-or-composite.xml")); LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[] { expected, real }); AssertJUnit.assertEquals(expected, real); } finally { close(session); } }
From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java
License:Apache License
@Test public void queryConnectorByType() throws Exception { Session session = open();/*from w ww . ja v a2 s. c om*/ try { Criteria main = session.createCriteria(RConnector.class, "c"); Criterion connectorType = Restrictions.conjunction() .add(Restrictions.eq("connectorType", "org.identityconnectors.ldap.LdapConnector")); main.add(connectorType); ProjectionList projections = Projections.projectionList(); addFullObjectProjectionList("c", projections, false); main.setProjection(projections); String expected = HibernateToSqlTranslator.toSql(main); String real = getInterpretedQuery(session, ConnectorType.class, new File(TEST_DIR, "query-connector-by-type.xml")); LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[] { expected, real }); AssertJUnit.assertEquals(expected, real); } finally { close(session); } }
From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java
License:Apache License
@Test public void queryAccountByAttributesAndResourceRef() throws Exception { Session session = open();/*from w w w . ja va 2 s. c o m*/ try { Criteria main = session.createCriteria(RShadow.class, "r"); Criteria stringAttr = main.createCriteria("strings", "s1x", JoinType.LEFT_OUTER_JOIN); //and Conjunction c1 = Restrictions.conjunction(); c1.add(Restrictions.eq("r.resourceRef.targetOid", "aae7be60-df56-11df-8608-0002a5d5c51b")); c1.add(Restrictions.eq("r.resourceRef.type", QNameUtil.qNameToUri(ResourceType.COMPLEX_TYPE))); //and Conjunction c2 = Restrictions.conjunction(); c2.add(Restrictions.eq("s1x.ownerType", RObjectExtensionType.ATTRIBUTES)); c2.add(Restrictions.eq("s1x.name", new QName("http://midpoint.evolveum.com/blabla", "foo"))); c2.add(Restrictions.eq("s1x.value", "uid=jbond,ou=People,dc=example,dc=com")); Conjunction conjunction = Restrictions.conjunction(); conjunction.add(c1); conjunction.add(c2); main.add(conjunction); ProjectionList projections = Projections.projectionList(); addFullObjectProjectionList("r", projections, false); main.setProjection(projections); String expected = HibernateToSqlTranslator.toSql(main); String real = getInterpretedQuery(session, ShadowType.class, new File(TEST_DIR, "query-account-by-attributes-and-resource-ref.xml")); LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[] { expected, real }); AssertJUnit.assertEquals(expected, real); } finally { close(session); } }
From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java
License:Apache License
@Test public void queryUserAccountRef() throws Exception { Session session = open();// www . j a va2 s . com try { Criteria main = session.createCriteria(RUser.class, "u"); ProjectionList projections = Projections.projectionList(); addFullObjectProjectionList("u", projections, false); main.setProjection(projections); Criteria refs = main.createCriteria("linkRef", "l", JoinType.LEFT_OUTER_JOIN); refs.add(Restrictions.conjunction().add(Restrictions.eq("l.targetOid", "123"))); String expected = HibernateToSqlTranslator.toSql(main); RefFilter filter = RefFilter.createReferenceEqual(UserType.F_LINK_REF, UserType.class, prismContext, "123"); String real = getInterpretedQuery(session, UserType.class, ObjectQuery.createObjectQuery(filter)); LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[] { expected, real }); AssertJUnit.assertEquals(expected, real); } finally { close(session); } }
From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java
License:Apache License
@Test public void asdf() throws Exception { Session session = open();// www .j av a 2s. c om try { Criteria main = session.createCriteria(RUser.class, "u"); Criteria a = main.createCriteria("assignments", "a"); a.add(Restrictions.eq("a.assignmentOwner", RAssignmentOwner.FOCUS)); Criteria e = a.createCriteria("a.extension"); Criteria s = e.createCriteria("strings", "s"); Conjunction c2 = Restrictions.conjunction(); c2.add(Restrictions.eq("s.extensionType", RAssignmentExtensionType.EXTENSION)); c2.add(Restrictions.eq("s.name", new QName("http://midpoint.evolveum.com/blabla", "foo"))); c2.add(Restrictions.eq("s.value", "uid=jbond,ou=People,dc=example,dc=com")); Conjunction c1 = Restrictions.conjunction(); c1.add(Restrictions.eq("a.targetRef.targetOid", "1234")); c1.add(Restrictions.eq("a.targetRef.type", RObjectType.ORG)); main.add(Restrictions.and(c1, c2)); main.setProjection(Projections.property("u.fullObject")); String expected = HibernateToSqlTranslator.toSql(main); LOGGER.info(">>> >>> {}", expected); } finally { close(session); } }
From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java
License:Apache License
@Test public void test310QueryNameAndOrg() throws Exception { Session session = open();/*from w w w .j av a 2s .c o m*/ try { DetachedCriteria detached = DetachedCriteria.forClass(ROrgClosure.class, "cl"); detached.setProjection(Projections.distinct(Projections.property("cl.descendantOid"))); detached.add(Restrictions.eq("cl.ancestorOid", "1234")); detached.add(Restrictions.ne("cl.descendantOid", "1234")); Criteria main = session.createCriteria(RUser.class, "u"); String mainAlias = "u"; ProjectionList projections = Projections.projectionList(); projections.add(Projections.property("fullObject")); projections.add(Projections.property("stringsCount")); projections.add(Projections.property("longsCount")); projections.add(Projections.property("datesCount")); projections.add(Projections.property("referencesCount")); projections.add(Projections.property("polysCount")); main.setProjection(projections); Conjunction c = Restrictions.conjunction(); c.add(Restrictions.and(Restrictions.eq("u.name.orig", "cpt. Jack Sparrow"), Restrictions.eq("u.name.norm", "cpt jack sparrow"))); c.add(Subqueries.propertyIn(mainAlias + ".oid", detached)); main.add(c); main.addOrder(Order.asc("u.name.orig")); String expected = HibernateToSqlTranslator.toSql(main); EqualFilter eqFilter = EqualFilter.createEqual(ObjectType.F_NAME, ObjectType.class, prismContext, null, new PolyString("cpt. Jack Sparrow", "cpt jack sparrow")); OrgFilter orgFilter = OrgFilter.createOrg("12341234-1234-1234-1234-123412341234"); ObjectQuery query = ObjectQuery.createObjectQuery(AndFilter.createAnd(eqFilter, orgFilter)); query.setPaging(ObjectPaging.createPaging(null, null, ObjectType.F_NAME, OrderDirection.ASCENDING)); String real = getInterpretedQuery(session, UserType.class, query); LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[] { expected, real }); AssertJUnit.assertEquals(expected, real); } finally { close(session); } }
From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java
License:Apache License
@Test public void test370queryObjectypeByTypeUserAndLocality() throws Exception { Session session = open();// w w w . j ava 2s . c om try { Criteria main = session.createCriteria(RObject.class, "o"); ProjectionList projections = Projections.projectionList(); addFullObjectProjectionList("o", projections, false); main.setProjection(projections); Conjunction c = Restrictions.conjunction(); main.add(c); c.add(Restrictions.eq("o." + RObject.F_OBJECT_TYPE_CLASS, RObjectType.USER)); c.add(Restrictions.and(Restrictions.eq("o.localityUser.orig", "Caribbean"), Restrictions.eq("o.localityUser.norm", "caribbean"))); String expected = HibernateToSqlTranslator.toSql(main); EqualFilter eq = EqualFilter.createEqual(new ItemPath(UserType.F_LOCALITY), UserType.class, prismContext, new PolyString("Caribbean", "caribbean")); TypeFilter type = TypeFilter.createType(UserType.COMPLEX_TYPE, eq); String real = getInterpretedQuery(session, ObjectType.class, ObjectQuery.createObjectQuery(type)); LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[] { expected, real }); AssertJUnit.assertEquals(expected, real); checkQueryTypeAlias(real, "m_user", "locality_orig", "locality_norm"); } finally { close(session); } }