List of usage examples for org.hibernate Criteria setFetchMode
public Criteria setFetchMode(String associationPath, FetchMode mode) throws HibernateException;
From source file:de.appsolve.padelcampus.db.dao.generic.BaseEntityDAO.java
@SuppressWarnings("unchecked") @Override/*from www . jav a 2s.c o m*/ public Page<T> findAllFetchEagerly(Pageable pageable, Set<Criterion> criterions, String... associations) { //http://stackoverflow.com/questions/2183617/criteria-api-returns-a-too-small-resultset //get the ids of every object that matches the pageable conditions //we cannot get the objects directly because we use FetchMode.JOIN which returns the scalar product of all rows in all affected tables //and CriteriaSpecification.DISTINCT_ROOT_ENTITY does not work on SQL Level but on in Java after the result is returned from SQL Criteria criteria = getPageableCriteria(pageable); if (criterions != null) { for (Criterion c : criterions) { criteria.add(c); } } criteria.setProjection(Projections.distinct(Projections.property("id"))); criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); List<Long> list = criteria.list(); //once we have the required ids we query for the complete objects Criteria objectCriteria = getCriteria(); for (String association : associations) { objectCriteria.setFetchMode(association, FetchMode.JOIN); } if (!list.isEmpty()) { objectCriteria.add(Restrictions.in("id", list)); } objectCriteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); addOrderBy(objectCriteria, pageable); List<T> objects = objectCriteria.list(); sort(objects); //we also need the total number of rows Criteria rowCountCritieria = getCriteria(); if (criterions != null) { for (Criterion c : criterions) { rowCountCritieria.add(c); } } rowCountCritieria.setProjection(Projections.rowCount()); Long resultCount = (Long) rowCountCritieria.uniqueResult(); if (resultCount == null) { resultCount = objects.size() + 0L; } Collections.sort(objects); PageImpl<T> page = new PageImpl<>(new ArrayList<>(objects), pageable, resultCount); return page; }
From source file:de.appsolve.padelcampus.db.dao.generic.BaseEntityDAO.java
@Override public Page<T> findAllByFuzzySearch(String search, Set<Criterion> criterions, String... associations) { Criteria criteria = getCriteria(); for (String association : associations) { criteria.setFetchMode(association, FetchMode.JOIN); }/*from w w w .j ava 2 s. co m*/ List<Criterion> predicates = new ArrayList<>(); for (String indexedPropery : getIndexedProperties()) { if (!StringUtils.isEmpty(search)) { String[] searchTerms = search.split(" "); for (String searchTerm : searchTerms) { predicates.add(Restrictions.ilike(indexedPropery, searchTerm, MatchMode.ANYWHERE)); } } } if (!predicates.isEmpty()) { criteria.add(Restrictions.or(predicates.toArray(new Criterion[predicates.size()]))); } if (criterions != null) { for (Criterion c : criterions) { criteria.add(c); } } criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); @SuppressWarnings("unchecked") List<T> objects = criteria.list(); sort(objects); PageImpl<T> page = new PageImpl<>(objects); return page; }
From source file:de.appsolve.padelcampus.db.dao.generic.BaseEntityDAO.java
@Override @SuppressWarnings("unchecked") public T findByUUIDFetchEagerly(final String uuid, String... associations) { Criteria criteria = getCriteria(); for (String association : associations) { criteria.setFetchMode(association, FetchMode.JOIN); }//from ww w . java 2 s . c o m criteria.add(Property.forName("UUID").eq(uuid)); return (T) criteria.uniqueResult(); }
From source file:de.appsolve.padelcampus.db.dao.generic.BaseEntityDAO.java
@Override @SuppressWarnings("unchecked") public T findByIdFetchEagerly(final long id, String... associations) { Criteria criteria = getCriteria(); for (String association : associations) { criteria.setFetchMode(association, FetchMode.JOIN); }// www . j av a 2s . c om criteria.add(Property.forName("id").eq(id)); return (T) criteria.uniqueResult(); }
From source file:de.appsolve.padelcampus.db.dao.generic.BaseEntityDAO.java
@Override public List<T> findAllFetchEagerly(String... associations) { Criteria crit = getCriteria(); for (String association : associations) { crit.setFetchMode(association, FetchMode.JOIN); }/* www .jav a 2 s.c o m*/ //we only want unique results //see http://stackoverflow.com/questions/18753245/one-to-many-relationship-gets-duplicate-objects-whithout-using-distinct-why crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); @SuppressWarnings("unchecked") List<T> list = (List<T>) crit.list(); sort(list); return list; }
From source file:de.appsolve.padelcampus.db.dao.generic.BaseEntityDAO.java
@Override public List<T> findAllFetchEagerlyWithAttributes(Map<String, Object> attributeMap, String... associations) { Criteria crit = getCriteria(); for (String association : associations) { crit.setFetchMode(association, FetchMode.JOIN); }/*from ww w .ja v a 2 s . c om*/ for (Map.Entry<String, Object> entry : attributeMap.entrySet()) { crit.add(Restrictions.eq(entry.getKey(), entry.getValue())); } //we only want unique results //see http://stackoverflow.com/questions/18753245/one-to-many-relationship-gets-duplicate-objects-whithout-using-distinct-why crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); @SuppressWarnings("unchecked") List<T> list = (List<T>) crit.list(); sort(list); return list; }
From source file:de.decidr.model.commands.user.GetUserPropertiesCommand.java
License:Apache License
@SuppressWarnings("unchecked") @Override// w ww. ja v a 2s. c o m public void transactionAllowed(TransactionStartedEvent evt) throws TransactionException { users = new ArrayList<User>(0); if ((userIds == null) || userIds.isEmpty()) { return; } Criteria crit = evt.getSession().createCriteria(User.class); for (String propertyToGet : propertiesToGet) { crit.setFetchMode(propertyToGet, FetchMode.JOIN); } crit.add(Restrictions.in("id", userIds)); users = crit.list(); }
From source file:de.iteratec.iteraplan.persistence.dao.GenericBaseDAO.java
License:Open Source License
/** * {@inheritDoc}//from w w w.ja v a2 s . com */ public E loadObjectById(final T id, final String... associations) { Preconditions.checkNotNull(id); HibernateCallback<E> callback = new HibernateCallback<E>() { @SuppressWarnings("unchecked") public E doInHibernate(Session session) { Criteria criteria = session.createCriteria(getPersistentClass()).add(Restrictions.idEq(id)); for (String association : associations) { criteria.setFetchMode(association, FetchMode.JOIN); } return (E) criteria.uniqueResult(); } }; return getHibernateTemplate().execute(callback); }
From source file:de.u808.simpleinquest.repository.impl.GenericHibernateDAO.java
License:Apache License
protected void setFetchModJoin(final String[] fetchList, Criteria criteria) { if (fetchList != null) { for (String aFetchList : fetchList) { criteria.setFetchMode(aFetchList, FetchMode.JOIN); }//from w w w .j a v a2s. c o m } }
From source file:edu.duke.cabig.c3pr.dao.ParticipantDao.java
License:BSD License
/** * Searches based on an example object. Typical usage from your service class: - If you want * to search based on diseaseCode, monitorCode, * <li><code>Participant participant = new Participant();</li></code> * <li>code>participant.setLastName("last_namee");</li> * </code>//from ww w.ja v a2 s .com * <li>code>participantDao.searchByExample(study)</li> * </code> * * @param participant the participant * @param isWildCard the is wild card * @param useAddress if set to true and {@link Address} is present, use it for the search. * @param useContactInfo if set to true and {@link ContactMechanism} is present, use it for the search. * @return list of matching participant objects based on your sample participant object */ public List<Participant> searchByExample(Participant participant, boolean isWildCard, boolean useAddress, boolean useContactInfo, boolean forceEager) { Example example = Example.create(participant).excludeZeroes().ignoreCase(); Criteria participantCriteria = getSession().createCriteria(Participant.class) .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); example.excludeProperty("doNotUse").enableLike(MatchMode.ANYWHERE); if (forceEager) { // TODO: The following lines may cause Participant.identifiers to contain duplicate entries! // In order to avoid that, Participant.identifiers will need to be changed from List to Set. // Related to http://www.jroller.com/eyallupu/entry/hibernate_exception_simultaneously_fetch_multiple. participantCriteria.setFetchMode("identifiers", FetchMode.JOIN); participantCriteria.setFetchMode("raceCodeAssociations", FetchMode.JOIN); participantCriteria.setFetchMode("contactMechanisms", FetchMode.JOIN); } if (isWildCard) { participantCriteria.add(example); if (participant.getIdentifiers().size() > 0) { Criterion identifierValueCriterion = Restrictions.ilike("value", "%" + participant.getIdentifiers().get(0).getValue() + "%"); Criterion identifierTypeCriterion = Restrictions.ilike("typeInternal", "%" + participant.getIdentifiers().get(0).getTypeInternal() + "%"); if (participant.getIdentifiers().get(0) instanceof SystemAssignedIdentifier) { Criterion identifierSourceCriterion = Restrictions.ilike("systemName", "%" + ((SystemAssignedIdentifier) participant.getIdentifiers().get(0)).getSystemName() + "%"); participantCriteria.createCriteria("identifiers").add(identifierValueCriterion) .add(identifierSourceCriterion).add(identifierTypeCriterion); } else { Criteria identifiersCriteria = participantCriteria.createCriteria("identifiers"); identifiersCriteria.add(identifierValueCriterion).add(identifierTypeCriterion); if (((OrganizationAssignedIdentifier) participant.getIdentifiers().get(0)).getHealthcareSite() .getIdentifiersAssignedToOrganization().size() > 0) { Criteria organizationCriteria = identifiersCriteria.createCriteria("healthcareSite"); organizationCriteria.createCriteria("identifiersAssignedToOrganization") .add(Restrictions.ilike("value", "%" + ((OrganizationAssignedIdentifier) participant.getIdentifiers().get(0)) .getHealthcareSite().getIdentifiersAssignedToOrganization().get(0) .getValue() + "%")); } } for (int i = 1; i < participant.getIdentifiers().size(); i++) { identifierValueCriterion = Restrictions.or(identifierValueCriterion, Restrictions.ilike("value", "%" + participant.getIdentifiers().get(i).getValue() + "%")); } } final Address address = participant.getAddressInternal(); if (useAddress && address != null) { final Criteria addrCrit = participantCriteria.createCriteria("addresses"); if (StringUtils.isNotBlank(address.getStreetAddress())) addrCrit.add(Restrictions.ilike("streetAddress", "%" + address.getStreetAddress() + "%")); if (StringUtils.isNotBlank(address.getCity())) addrCrit.add(Restrictions.ilike("city", "%" + address.getCity() + "%")); if (StringUtils.isNotBlank(address.getStateCode())) addrCrit.add(Restrictions.ilike("stateCode", "%" + address.getStateCode() + "%")); if (StringUtils.isNotBlank(address.getCountryCode())) addrCrit.add(Restrictions.ilike("countryCode", "%" + address.getCountryCode() + "%")); if (StringUtils.isNotBlank(address.getPostalCode())) addrCrit.add(Restrictions.ilike("postalCode", "%" + address.getPostalCode() + "%")); } if (useContactInfo) { List<Criterion> criterions = new ArrayList<Criterion>(); if (StringUtils.isNotBlank(participant.getEmail())) { criterions.add( Example.create(new ContactMechanism(ContactMechanismType.EMAIL, participant.getEmail())) .enableLike().ignoreCase()); } if (StringUtils.isNotBlank(participant.getPhone())) { criterions.add( Example.create(new ContactMechanism(ContactMechanismType.PHONE, participant.getPhone())) .enableLike().ignoreCase()); } if (StringUtils.isNotBlank(participant.getFax())) { criterions.add( Example.create(new ContactMechanism(ContactMechanismType.Fax, participant.getFax())) .enableLike().ignoreCase()); } if (!criterions.isEmpty()) { Disjunction disjunction = Restrictions.disjunction(); for (Criterion criterion : criterions) { disjunction.add(criterion); } final Criteria contactCrit = participantCriteria.createCriteria("contactMechanisms"); contactCrit.add(disjunction); } } return participantCriteria.list(); } return participantCriteria.add(example).list(); }