List of usage examples for org.hibernate FetchMode JOIN
FetchMode JOIN
To view the source code for org.hibernate FetchMode JOIN.
Click Source Link
From source file:com.arg.arsoft.siantluis.repository.imp.SupplierRepository.java
@Override public Map findByQuery(SupplierQuery query) { int pageSize = Configs.PAGE_SIZE; Criteria criteria = factory.getCurrentSession().createCriteria(Supplier.class); // criteria.createAlias("address.amphur","a",JoinType.LEFT_OUTER_JOIN); if (query.getCode() != null && !query.getCode().equals("")) { if (query.getCode().contains("*") || query.getCode().contains("?")) { criteria.add(Expression.like("code", query.getCode().replace("*", "%").replace("?", "_"))); } else {/*from w ww . j a v a 2 s .co m*/ criteria.add(Expression.eq("code", query.getCode())); } } if (query.getName() != null && !query.getName().equals("")) { if (query.getName().contains("*") || query.getName().contains("?")) { criteria.add(Expression.like("name", query.getName().replace("*", "%").replace("?", "_"))); } else { criteria.add(Expression.eq("name", query.getName())); } } if (query.getCompany() != null && !query.getCompany().equals("")) { if (query.getCompany().contains("*") || query.getCompany().contains("?")) { criteria.add(Expression.like("compny", query.getCompany().replace("*", "%").replace("?", "_"))); } else { criteria.add(Expression.eq("compny", query.getCompany())); } } if (query.getContactName() != null && !query.getContactName().equals("")) { if (query.getContactName().contains("*") || query.getContactName().contains("?")) { criteria.add(Expression.like("contact.contactName", query.getContactName().replace("*", "%").replace("?", "_"))); } else { criteria.add(Expression.eq("contact.contactName", query.getContactName())); } } if (query.getProvince() != null && query.getProvince() != 0) { criteria.createAlias("address.province", "p", JoinType.LEFT_OUTER_JOIN); criteria.add(Expression.eq("p.id", query.getProvince())); } long totalRecord = (long) criteria.setProjection(Projections.count(Projections.id().toString())) .uniqueResult(); criteria.setProjection(null); int start = ((query.getPage() - 1) * pageSize); criteria.setFetchMode("address.province", FetchMode.JOIN); criteria.setFetchMode("address.amphur", FetchMode.JOIN); List<Supplier> result = criteria.setFirstResult(start).setMaxResults(pageSize) .setResultTransformer(Criteria.ROOT_ENTITY).list(); long totalPage = totalRecord / pageSize; if ((totalRecord % pageSize) > 0) { totalPage++; } List<Integer> pages = new ArrayList<Integer>(); for (int index = 1; index <= totalPage; index++) { pages.add(index); } Map data = new HashMap(); data.put("list", result); data.put("totalPage", totalPage); data.put("totalRecord", totalRecord); data.put("pages", pages); System.out.println(pages.size()); return data; }
From source file:com.arg.arsoft.siantluis.repository.imp.UserRepository.java
@Override public User findByKey(Integer key) { Criteria criteria = factory.getCurrentSession().createCriteria(User.class); criteria.add(Restrictions.eq("id", key)); criteria.setFetchMode("roles", FetchMode.JOIN); List<User> result = criteria.list(); if (result != null && result.size() > 0) { return result.get(0); } else {//from w ww. j a v a 2 s . c o m return null; } }
From source file:com.ephesoft.dcma.da.dao.hibernate.FieldTypeDaoImpl.java
License:Open Source License
/** * An API to fetch all Field types by document type name. * /* w ww . j a va2 s. c o m*/ * @param docTypeName String * @param batchInstanceIdentifier String * @param isKVExtraction boolean * @return List<FieldType> */ @Override public List<FieldType> getFdTypeByDocumentTypeName(String docTypeName, String batchInstanceIdentifier, boolean isKVExtraction) { LOG.info("batchInstanceID ID : " + batchInstanceIdentifier); DetachedCriteria criteria = criteria(); criteria.createAlias(DOC_TYPE, DOC_TYPE, JoinFragment.INNER_JOIN); criteria.add(Restrictions.eq(DOC_TYPE_NAME, docTypeName)); criteria.createAlias(DOC_TYPE_BATCH_CLASS, BATCH_CLASS1, JoinFragment.INNER_JOIN); if (isKVExtraction) { criteria.setFetchMode("kvExtraction", FetchMode.JOIN); criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); } DetachedCriteria subQuery = criteria(BatchInstance.class); subQuery.add(Restrictions.eq(IDENTIFIER, batchInstanceIdentifier)); subQuery.createAlias(BATCH_CLASS, BATCH_CLASS2, JoinFragment.INNER_JOIN); subQuery.setProjection(Projections.property(BATCH_CLASS2_IDENTIFIER)); criteria.add(Subqueries.propertyEq(BATCH_CLASS1_IDENTIFIER, subQuery)); criteria.addOrder(org.hibernate.criterion.Order.asc(FIELD_ORDER_NUMBER)); return find(criteria); }
From source file:com.ephesoft.dcma.da.dao.hibernate.FieldTypeDaoImpl.java
License:Open Source License
/** * An API to fetch all Field types by document type name. * /*from w ww. j a v a 2s. c o m*/ * @param docTypeName String * @param batchInstanceIdentifier String * @return List<FieldType> */ @Override public List<FieldType> getFdTypeAndRegexValidationByDocTypeName(String docTypeName, String batchInstanceIdentifier) { LOG.info("batchInstanceID ID : " + batchInstanceIdentifier); DetachedCriteria criteria = criteria(); criteria.createAlias(DOC_TYPE, DOC_TYPE, JoinFragment.INNER_JOIN); criteria.add(Restrictions.eq(DOC_TYPE_NAME, docTypeName)); criteria.createAlias(DOC_TYPE_BATCH_CLASS, BATCH_CLASS1, JoinFragment.INNER_JOIN); criteria.setFetchMode(REGEX_VALIDATION, FetchMode.JOIN); criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); DetachedCriteria subQuery = criteria(BatchInstance.class); subQuery.add(Restrictions.eq(IDENTIFIER, batchInstanceIdentifier)); subQuery.createAlias(BATCH_CLASS, BATCH_CLASS2, JoinFragment.INNER_JOIN); subQuery.setProjection(Projections.property(BATCH_CLASS2_IDENTIFIER)); criteria.add(Subqueries.propertyEq(BATCH_CLASS1_IDENTIFIER, subQuery)); criteria.addOrder(org.hibernate.criterion.Order.asc(FIELD_ORDER_NUMBER)); return find(criteria); }
From source file:com.eucalyptus.auth.DatabaseAuthProvider.java
License:Open Source License
public User lookupUserByEmailAddress(String email) throws AuthException { if (email == null || "".equals(email)) { throw new AuthException("Empty email address to search"); }//from ww w . j a v a 2s . c o m final EntityTransaction tx = Entities.get(UserEntity.class); try { final UserEntity match = (UserEntity) Entities.createCriteria(UserEntity.class).setCacheable(true) .createAlias("info", "i") .add(Restrictions.eq("i." + CollectionPropertyNames.COLLECTION_ELEMENTS, email).ignoreCase()) .setFetchMode("info", FetchMode.JOIN).setReadOnly(true).uniqueResult(); if (match == null) { throw new AuthException(AuthException.NO_SUCH_USER); } boolean emailMatched = false; Map<String, String> info = match.getInfo(); if (info != null) { for (Map.Entry<String, String> entry : info.entrySet()) { if (entry.getKey() != null && User.EMAIL.equals(entry.getKey()) && entry.getValue() != null && email.equalsIgnoreCase(entry.getValue())) { emailMatched = true; break; } } } if (!emailMatched) { throw new AuthException(AuthException.NO_SUCH_USER); } return new DatabaseUserProxy(match); } catch (AuthException e) { Debugging.logError(LOG, e, "Failed to find user by email address " + email); throw e; } catch (Exception e) { Debugging.logError(LOG, e, "Failed to find user by email address " + email); throw new AuthException(AuthException.NO_SUCH_USER, e); } finally { tx.rollback(); } }
From source file:com.eucalyptus.auth.euare.persist.DatabaseAuthProvider.java
License:Open Source License
@Override @SuppressWarnings("unchecked") public void deleteAccount(String accountName, boolean forceDeleteSystem, boolean recursive) throws AuthException { if (accountName == null) { throw new AuthException(AuthException.EMPTY_ACCOUNT_NAME); }//from w w w . j av a2 s. c o m if (!forceDeleteSystem && Accounts.isSystemAccount(accountName)) { throw new AuthException(AuthException.DELETE_SYSTEM_ACCOUNT); } if (!(recursive || DatabaseAuthUtils.isAccountEmpty(accountName))) { throw new AuthException(AuthException.ACCOUNT_DELETE_CONFLICT); } try (final TransactionResource db = Entities.transactionFor(AccountEntity.class)) { if (recursive) { List<UserEntity> users = (List<UserEntity>) Entities.createCriteria(UserEntity.class) .setFetchMode("info", FetchMode.JOIN).createCriteria("groups") .add(Restrictions.eq("userGroup", true)).createCriteria("account") .add(Restrictions.eq("name", accountName)).list(); for (UserEntity u : users) { Entities.delete(u); } List<InstanceProfileEntity> profiles = (List<InstanceProfileEntity>) Entities .createCriteria(InstanceProfileEntity.class).createCriteria("account") .add(Restrictions.eq("name", accountName)).list(); for (InstanceProfileEntity p : profiles) { Entities.delete(p); } List<RoleEntity> roles = (List<RoleEntity>) Entities.createCriteria(RoleEntity.class) .setFetchMode("policies", FetchMode.JOIN).createCriteria("account") .add(Restrictions.eq("name", accountName)).list(); for (RoleEntity r : roles) { Entities.delete(r); } List<GroupEntity> groups = (List<GroupEntity>) Entities.createCriteria(GroupEntity.class) .setFetchMode("policies", FetchMode.JOIN).createCriteria("account") .add(Restrictions.eq("name", accountName)).list(); for (GroupEntity g : groups) { Entities.delete(g); } } AccountEntity account = (AccountEntity) Entities.createCriteria(AccountEntity.class).setCacheable(true) .add(Restrictions.eq("name", accountName)).uniqueResult(); if (account == null) { throw new NoSuchElementException("Can not find account " + accountName); } Entities.delete(account); db.commit(); } catch (Exception e) { Debugging.logError(LOG, e, "Failed to delete account " + accountName); throw new AuthException(AuthException.NO_SUCH_ACCOUNT, e); } }
From source file:com.eucalyptus.auth.euare.persist.DatabaseAuthProvider.java
License:Open Source License
public EuareUser lookupUserByEmailAddress(String email) throws AuthException { if (email == null || "".equals(email)) { throw new AuthException("Empty email address to search"); }/*from www . j a v a2s . c o m*/ final EntityTransaction tx = Entities.get(UserEntity.class); try { final UserEntity match = (UserEntity) Entities.createCriteria(UserEntity.class).setCacheable(true) .createAlias("info", "i") .add(Restrictions.eq("i." + CollectionPropertyNames.COLLECTION_ELEMENTS, email).ignoreCase()) .setFetchMode("info", FetchMode.JOIN).setReadOnly(true).uniqueResult(); if (match == null) { throw new AuthException(AuthException.NO_SUCH_USER); } boolean emailMatched = false; Map<String, String> info = match.getInfo(); if (info != null) { for (Map.Entry<String, String> entry : info.entrySet()) { if (entry.getKey() != null && User.EMAIL.equals(entry.getKey()) && entry.getValue() != null && email.equalsIgnoreCase(entry.getValue())) { emailMatched = true; break; } } } if (!emailMatched) { throw new AuthException(AuthException.NO_SUCH_USER); } return new DatabaseUserProxy(match); } catch (AuthException e) { Debugging.logError(LOG, e, "Failed to find user by email address " + email); throw e; } catch (Exception e) { Debugging.logError(LOG, e, "Failed to find user by email address " + email); throw new AuthException(AuthException.NO_SUCH_USER, e); } finally { tx.rollback(); } }
From source file:com.evolveum.midpoint.repo.sql.OrgStructTest.java
License:Apache License
@SuppressWarnings({ "unchecked" }) @Test/* ww w .j av a 2 s . c o m*/ public void test001addOrgStructObjectsIncorrect() throws Exception { LOGGER.info("===[ addIncorrectOrgStruct ]==="); OperationResult opResult = new OperationResult("===[ addIncorrectOrgStruct ]==="); List<PrismObject<? extends Objectable>> orgStructIncorrect = prismContext .parseObjects(new File(ORG_STRUCT_OBJECTS_INCORRECT)); for (PrismObject<? extends Objectable> o : orgStructIncorrect) { repositoryService.addObject((PrismObject<ObjectType>) o, null, opResult); } OrgType orgF008 = repositoryService.getObject(OrgType.class, ORG_F008_OID, null, opResult).asObjectable(); OrgType orgF009 = repositoryService.getObject(OrgType.class, ORG_F009_OID, null, opResult).asObjectable(); AssertJUnit.assertEquals(orgF008.getParentOrgRef().get(0).getOid(), orgF009.getOid()); OrgType orgF010 = repositoryService.getObject(OrgType.class, ORG_F010_OID, null, opResult).asObjectable(); ObjectReferenceType oRefType = new ObjectReferenceType(); boolean isEqual009 = false; boolean isEqual012 = false; for (int i = 0; i < orgF010.getParentOrgRef().size(); i++) { oRefType = orgF010.getParentOrgRef().get(i); isEqual009 = isEqual009 || oRefType.getOid().equals(orgF009.getOid()); isEqual012 = isEqual012 || oRefType.getOid().equals(ORG_F012_OID); } AssertJUnit.assertEquals(true, isEqual009); AssertJUnit.assertEquals(true, isEqual012); LOGGER.info("==>after add<=="); Session session = getFactory().openSession(); session.beginTransaction(); LOGGER.info("==============CLOSURE TABLE=========="); List<ROrgClosure> orgClosure = session.createQuery("from ROrgClosure").list(); // descendants of F007 - F007<0>, F009<1>, F008<2>, F0010<2> Criteria criteria = session.createCriteria(ROrgClosure.class).createCriteria("ancestor", "anc") .setFetchMode("ancestor", FetchMode.JOIN).add(Restrictions.eq("anc.oid", ORG_F007_OID)); orgClosure = criteria.list(); AssertJUnit.assertEquals(5, orgClosure.size()); criteria = session.createCriteria(ROrgClosure.class).add(Restrictions.eq("depth", 2)) .createCriteria("ancestor", "anc").setFetchMode("ancestor", FetchMode.JOIN) .add(Restrictions.eq("anc.oid", ORG_F007_OID)); orgClosure = criteria.list(); AssertJUnit.assertEquals(3, orgClosure.size()); LOGGER.info("==============ORG INCORRECT TABLE=========="); List<ROrgIncorrect> orgIncorrect = session.createQuery("from ROrgIncorrect").list(); AssertJUnit.assertEquals(1, orgIncorrect.size()); AssertJUnit.assertEquals(ORG_F012_OID, orgIncorrect.get(0).getAncestorOid()); AssertJUnit.assertEquals(ORG_F010_OID, orgIncorrect.get(0).getDescendantOid()); ObjectQuery query = new ObjectQuery(); PrismObjectDefinition<UserType> userObjectDef = prismContext.getSchemaRegistry() .findObjectDefinitionByCompileTimeClass(UserType.class); query.setFilter(EqualFilter.createEqual(UserType.F_NAME, userObjectDef.findPropertyDefinition(UserType.F_NAME), null, ELAINE_NAME1)); List<PrismObject<UserType>> users = repositoryService.searchObjects(UserType.class, query, null, opResult); AssertJUnit.assertNotNull(users); AssertJUnit.assertEquals(1, users.size()); UserType elaine1 = users.get(0).asObjectable(); LOGGER.info("--->elaine1<----"); // LOGGER.info(prismContext.silentMarshalObject(elaine1, LOGGER)); AssertJUnit.assertEquals("Expected name elaine, but got " + elaine1.getName().getOrig(), "elaine1", elaine1.getName().getOrig()); AssertJUnit.assertEquals("Expected elaine has one org ref, but got " + elaine1.getParentOrgRef().size(), 2, elaine1.getParentOrgRef().size()); AssertJUnit.assertEquals("Parent org ref oid not equal.", "00000000-8888-6666-0000-100000000011", elaine1.getParentOrgRef().get(0).getOid()); session.getTransaction().commit(); session.close(); }
From source file:com.evolveum.midpoint.repo.sql.OrgStructTest.java
License:Apache License
@SuppressWarnings("unchecked") @Test/*ww w.j a v a 2s .c o m*/ public void test002modifyOrgStructAddRef() throws Exception { LOGGER.info("===[ modifyOrgStruct ]==="); OperationResult opResult = new OperationResult("===[ modifyOrgStruct ]==="); // test modification of org ref in another org type.. ObjectModificationType modification = PrismTestUtil.parseAtomicValue(new File(MODIFY_ORG_ADD_REF_FILENAME), ObjectModificationType.COMPLEX_TYPE); ObjectDelta<OrgType> delta = DeltaConvertor.createObjectDelta(modification, OrgType.class, prismContext); Session session = getFactory().openSession(); session.beginTransaction(); Criteria criteria = session.createCriteria(ROrgClosure.class).createCriteria("descendant", "desc") .setFetchMode("descendant", FetchMode.JOIN) .add(Restrictions.eq("desc.oid", MODIFY_ORG_ADD_REF_OID)); List<ROrgClosure> orgClosure = criteria.list(); LOGGER.info("before modify"); for (ROrgClosure c : orgClosure) { LOGGER.info("{}\t{}\t{}", new Object[] { c.getAncestor().getOid(), c.getDescendant().getOid(), c.getDepth() }); } AssertJUnit.assertEquals(3, orgClosure.size()); session.getTransaction().commit(); repositoryService.modifyObject(OrgType.class, MODIFY_ORG_ADD_REF_OID, delta.getModifications(), opResult); session.clear(); session.beginTransaction(); criteria = session.createCriteria(ROrgClosure.class).createCriteria("descendant", "desc") .setFetchMode("descendant", FetchMode.JOIN) .add(Restrictions.eq("desc.oid", MODIFY_ORG_ADD_REF_OID)); orgClosure = criteria.list(); LOGGER.info("after modify"); for (ROrgClosure c : orgClosure) { LOGGER.info("{}\t{}\t{}", new Object[] { c.getAncestor().getOid(), c.getDescendant().getOid(), c.getDepth() }); } AssertJUnit.assertEquals(4, orgClosure.size()); List<String> ancestors = new ArrayList<String>(); ancestors.add(MODIFY_ORG_ADD_REF_OID); ancestors.add(ORG_F003_OID); ancestors.add(ORG_F001_OID); ancestors.add(ORG_F002_OID); for (String ancestorOid : ancestors) { criteria = session.createCriteria(ROrgClosure.class); criteria.createCriteria("ancestor", "anc").setFetchMode("ancestor", FetchMode.JOIN) .add(Restrictions.eq("anc.oid", ancestorOid)); criteria.createCriteria("descendant", "desc").setFetchMode("descendant", FetchMode.JOIN) .add(Restrictions.eq("desc.oid", MODIFY_ORG_ADD_REF_OID)); orgClosure = criteria.list(); LOGGER.info("=> A: {}, D: {}, depth: {}", new Object[] { orgClosure.get(0).getAncestor().toJAXB(prismContext, null), orgClosure.get(0).getDescendant().toJAXB(prismContext, null), orgClosure.get(0).getDepth() }); AssertJUnit.assertEquals(1, orgClosure.size()); AssertJUnit.assertEquals(ancestorOid, orgClosure.get(0).getAncestor().getOid()); AssertJUnit.assertEquals(MODIFY_ORG_ADD_REF_OID, orgClosure.get(0).getDescendant().getOid()); int depth = -1; if (ancestorOid.equals(MODIFY_ORG_ADD_REF_OID)) { depth = 0; } else if (ancestorOid.equals(ORG_F001_OID)) { depth = 2; } else if (ancestorOid.equals(ORG_F003_OID)) { depth = 1; } else if (ancestorOid.equals(ORG_F002_OID)) { depth = 1; } AssertJUnit.assertEquals(depth, orgClosure.get(0).getDepth()); } session.getTransaction().commit(); session.close(); }
From source file:com.evolveum.midpoint.repo.sql.OrgStructTest.java
License:Apache License
@SuppressWarnings("unchecked") @Test//from w w w. j a v a 2 s.c om public void test002modifyOrgStructAddRefIncorrect() throws Exception { LOGGER.info("===[ modifyOrgStruct ]==="); OperationResult opResult = new OperationResult("===[ modifyOrgStructIncorrect ]==="); // test modification of org ref in another org type.. ObjectModificationType modification = PrismTestUtil.parseAtomicValue( new File(MODIFY_ORG_INCORRECT_ADD_REF_FILENAME), ObjectModificationType.COMPLEX_TYPE); ObjectDelta<OrgType> delta = DeltaConvertor.createObjectDelta(modification, OrgType.class, prismContext); repositoryService.modifyObject(OrgType.class, MODIFY_ORG_INCORRECT_ADD_REF_OID, delta.getModifications(), opResult); Session session = getFactory().openSession(); session.beginTransaction(); Criteria criteria = session.createCriteria(ROrgClosure.class).createCriteria("descendant", "desc") .setFetchMode("descendant", FetchMode.JOIN) .add(Restrictions.eq("desc.oid", MODIFY_ORG_INCORRECT_ADD_REF_OID)); List<ROrgClosure> orgClosure = criteria.list(); LOGGER.info("after modify incorrect - closure"); for (ROrgClosure c : orgClosure) { LOGGER.info("{}\t{}\t{}", new Object[] { c.getAncestor().getOid(), c.getDescendant().getOid(), c.getDepth() }); } AssertJUnit.assertEquals(5, orgClosure.size()); List<String> ancestors = new ArrayList<String>(); ancestors.add(MODIFY_ORG_INCORRECT_ADD_REF_OID); ancestors.add(ORG_F001_OID); ancestors.add(ORG_F002_OID); for (String ancestorOid : ancestors) { criteria = session.createCriteria(ROrgClosure.class); criteria.createCriteria("ancestor", "anc").setFetchMode("ancestor", FetchMode.JOIN) .add(Restrictions.eq("anc.oid", ancestorOid)); criteria.createCriteria("descendant", "desc").setFetchMode("descendant", FetchMode.JOIN) .add(Restrictions.eq("desc.oid", MODIFY_ORG_INCORRECT_ADD_REF_OID)); orgClosure = criteria.list(); AssertJUnit.assertEquals(1, orgClosure.size()); AssertJUnit.assertEquals(ancestorOid, orgClosure.get(0).getAncestor().getOid()); AssertJUnit.assertEquals(MODIFY_ORG_INCORRECT_ADD_REF_OID, orgClosure.get(0).getDescendant().getOid()); int depth = -1; if (ancestorOid.equals(MODIFY_ORG_INCORRECT_ADD_REF_OID)) { depth = 0; } else if (ancestorOid.equals(ORG_F001_OID)) { depth = 2; } else if (ancestorOid.equals(ORG_F002_OID)) { depth = 1; } AssertJUnit.assertEquals(depth, orgClosure.get(0).getDepth()); } criteria = session.createCriteria(ROrgIncorrect.class) .add(Restrictions.eq("descendantOid", MODIFY_ORG_INCORRECT_ADD_REF_OID)); List<ROrgIncorrect> orgIncorrect = criteria.list(); LOGGER.info("after modify incorrect - incorrect"); for (ROrgIncorrect c : orgIncorrect) { LOGGER.info("{}\t{}\t{}", new Object[] { c.getAncestorOid(), c.getDescendantOid() }); } AssertJUnit.assertEquals(1, orgIncorrect.size()); AssertJUnit.assertEquals(MODIFY_ORG_INCORRECT_ADD_REF_OID, orgIncorrect.get(0).getDescendantOid()); AssertJUnit.assertEquals(ORG_F012_OID, orgIncorrect.get(0).getAncestorOid()); session.getTransaction().commit(); session.close(); }