Example usage for org.hibernate FetchMode JOIN

List of usage examples for org.hibernate FetchMode JOIN

Introduction

In this page you can find the example usage for org.hibernate FetchMode JOIN.

Prototype

FetchMode JOIN

To view the source code for org.hibernate FetchMode JOIN.

Click Source Link

Document

Fetch using an outer join.

Usage

From source file:de.iteratec.iteraplan.persistence.dao.GenericBaseDAO.java

License:Open Source License

/**
 * {@inheritDoc}/*www.  jav  a 2 s  .co  m*/
 */
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  ww  w. j a  v  a  2  s. 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  w w  w .  ja va2s . 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();
}

From source file:edu.emory.library.tast.misc.tests.hibernate.QueryTest.java

License:Open Source License

public static void avoidingSelectsInCode() {

    Session session = HibernateConn.getSession();
    Transaction transaction = session.beginTransaction();

    List list = session.createCriteria(Book.class).setFetchMode("permissions", FetchMode.JOIN).list();
    for (Iterator iter = list.iterator(); iter.hasNext();) {
        Book b = (Book) iter.next();
        System.out.println(b.getAuthor().getName());
    }/*from  w ww .j av  a 2 s .  c o m*/

    transaction.commit();

}

From source file:edu.utah.further.core.data.hibernate.query.QueryBuilderHibernateImpl.java

License:Apache License

/**
 * Apply alias definitions of a search criterion to the corresponding Hibernate
 * criteria.//from   w w w  . j  a va  2 s.  co m
 * 
 * @param searchQuery
 * @param destination
 */
static void addAliases(final SearchQuery searchQuery, final GenericCriteria destination) {
    for (final SearchQueryAlias alias : searchQuery.getAliases()) {
        // Note the argument order reversing between the search framework
        // and Hibernate but not between our search framework and Hibernate adapters
        destination.addAlias(alias.getKey(), alias.getValue()).setFetchMode(alias.getValue(), FetchMode.JOIN);
    }
}

From source file:es.juntadeandalucia.panelGestion.persistencia.dao.impl.SchemaDAOImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*  w  w w.jav a2s .  c  o  m*/
public List<Schema> findByDataBase(long dataBaseId) {
    Criteria criteria = getSession().createCriteria(Schema.class);

    criteria.setFetchMode("dataBase", FetchMode.JOIN);
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    criteria.add(Restrictions.eq("dataBase.id", dataBaseId));

    return criteria.list();
}

From source file:es.juntadeandalucia.panelGestion.persistencia.dao.impl.TableDAOImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from ww w  .  j  ava 2s.  c  o m
public List<Table> findBySchema(long schemaId) {
    Criteria criteria = getSession().createCriteria(Table.class);

    criteria.setFetchMode("schema", FetchMode.JOIN);
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    criteria.add(Restrictions.eq("schema.id", schemaId));

    return criteria.list();
}

From source file:es.sm2.openppm.core.dao.ActivitysellerDAO.java

License:Open Source License

/**
 * Get Activity sellers from Project// ww w .  j  a v a2 s.c o m
 *
  * @param joins
  * @param projects
 * @return
 */
public List<Activityseller> consActivitySellerByProject(List<String> joins, Project... projects) {
    Criteria crit = getSession().createCriteria(getPersistentClass());

    for (String join : joins) {
        crit.setFetchMode(join, FetchMode.JOIN);
    }

    crit.addOrder(Order.asc(Activityseller.IDACTIVITYSELLER)).createCriteria(Activityseller.PROJECTACTIVITY)
            .add(Restrictions.in(Projectactivity.PROJECT, projects));

    return crit.list();
}

From source file:es.sm2.openppm.core.dao.ActivitysellerDAO.java

License:Open Source License

/**
 * Find seller associated project//from www.  ja  va2s  .c  o  m
 * @param projectactivity
 * @param joins
 * @return
 */
@SuppressWarnings("unchecked")
public List<Activityseller> findSellerAssociatedProject(Projectactivity projectactivity, List<String> joins) {
    Criteria crit = getSession().createCriteria(getPersistentClass());

    for (String join : joins) {
        crit.setFetchMode(join, FetchMode.JOIN);
    }

    crit.add(Restrictions.eq(Activityseller.PROJECTACTIVITY, projectactivity))
            .add(Restrictions.isNotNull(Activityseller.PROJECT));

    return crit.list();
}

From source file:es.sm2.openppm.core.dao.CalendarbaseDAO.java

License:Open Source License

/**
 * Return calendar base with exceptions//from w  ww  .j ava  2s.c o  m
 * @param calendarbase
 * @return
 */
public Calendarbase findByIdWithExceptions(Calendarbase calendarbase) {

    Criteria crit = getSession().createCriteria(getPersistentClass());
    crit.setFetchMode("calendarbaseexceptionses", FetchMode.JOIN);
    crit.add(Restrictions.eq("idCalendarBase", calendarbase.getIdCalendarBase()));

    return (Calendarbase) crit.uniqueResult();
}