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:helpers.OlymHelper.java

public static Korisnik getUserByName(String username) {//ne dohvata zemlju|| dohvata nakon setfetch stavljanja
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tr = null;//from  w w  w .ja  v a 2s . c o m
    Korisnik user = null;
    try {
        tr = session.beginTransaction();
        // Query q = session.createQuery("from Korisnik where username='" + username + "'");
        Criteria criteria = (Criteria) session.createCriteria(Korisnik.class).setFetchMode("zemlja",
                FetchMode.JOIN);
        criteria.add(Restrictions.eq("username", username));

        user = (Korisnik) criteria.uniqueResult();

        tr.commit();
    } catch (HibernateException e) {
        if (tr != null) {
            tr.rollback();
        }
    } finally {
        session.close();
    }
    return user;
}

From source file:helpers.OrgHelper.java

public static List<Spordisc> getSpordiscBySport(Sport sport) {//sport nije uhvatio, ostalo jeste
    Session session = HibernateUtil.getSessionFactory().openSession();//sa dodatkom fetchmode sve dohvatio
    Transaction tr = null;// w w  w. j  a  va  2s .  c o  m
    boolean good = false;
    List<Spordisc> sp = null;
    try {
        tr = session.beginTransaction();
        Criteria criteria = (Criteria) session.createCriteria(Spordisc.class)
                .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).setFetchMode("sport", FetchMode.JOIN);
        criteria.add(Restrictions.eq("sport", sport));
        sp = criteria.list();
        tr.commit();
        if (sp != null) {
            good = true;
        }
    } catch (HibernateException e) {
        if (tr != null) {
            tr.rollback();
        }
    } finally {
        session.close();
    }
    return sp;
}

From source file:helpers.OrgHelper.java

public static List<Korisnik> getDelegates() {//sve hib dohvata
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tr = null;//w  ww  .  j  a va2s .com
    List<Korisnik> user = null;
    boolean res = false;
    try {
        tr = session.beginTransaction();
        Criteria criteria = session.createCriteria(Korisnik.class).setFetchMode("zemlja", FetchMode.JOIN)
                .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        criteria.add(Restrictions.eq("tip", 1));
        criteria.add(Restrictions.eq("odobren", true));
        criteria.add(Restrictions.lt("nadgleda", 3));
        /*Query q = session.createQuery("from Korisnik where tip=1 and odobren=1 and nadgleda<3");
        user = q.list();*/
        user = criteria.list();
        tr.commit();

        if (user != null) {
            res = true;
        }
    } catch (HibernateException e) {
        if (tr != null) {
            tr.rollback();
        }
    } finally {
        session.close();
    }
    return user;

}

From source file:helpers.OrgHelper.java

public static Spordisc getSporDiscById(int id) {//ne dohvata sport, hoce sa fetch mode
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tr = null;/*w w w . j  av  a2 s. c  o m*/
    Spordisc dc = null;
    try {
        tr = session.beginTransaction();
        Criteria criteria = (Criteria) session.createCriteria(Spordisc.class).setFetchMode("sport",
                FetchMode.JOIN);
        criteria.add(Restrictions.eq("id", id));
        dc = (Spordisc) criteria.uniqueResult();
        // Hibernate.initialize(dc.getSport());
        tr.commit();
    } catch (HibernateException e) {
        if (tr != null) {
            tr.rollback();
        }
    } finally {
        session.close();
    }
    return dc;
}

From source file:helpers.OrgHelper.java

public static List<Korisnik> getUsersToApp() {//ne popunjava im zemlju|| hoce sa criterionom i fetchmodeom i transformerom
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tr = null;//from   w  w  w .  j  a v  a 2  s  . c  o m
    List<Korisnik> user = null;
    boolean good = false;
    try {
        tr = session.beginTransaction();
        Criteria criteria = (Criteria) session.createCriteria(Korisnik.class)
                .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).setFetchMode("zemlja", FetchMode.JOIN);
        criteria.add(Restrictions.eq("odobren", false));
        user = criteria.list();
        //  Query q = session.createQuery("from Korisnik where odobren=0");
        //  user = q.list();
        tr.commit();
        good = true;
    } catch (HibernateException e) {
        if (tr != null) {
            tr.rollback();
        }
    } finally {
        session.close();
    }
    return user;
}

From source file:info.jtrac.hibernate.HibernateJtracDao.java

License:Apache License

public List<Item> findAllItems(final int firstResult, final int batchSize) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(Session session) {
            session.clear();//from   w ww.ja v  a 2 s.c  o  m
            Criteria criteria = session.createCriteria(Item.class);
            criteria.setCacheMode(CacheMode.IGNORE);
            criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
            criteria.setFetchMode("history", FetchMode.JOIN);
            criteria.add(Restrictions.ge("id", (long) firstResult));
            criteria.add(Restrictions.lt("id", (long) firstResult + batchSize));
            return criteria.list();
        }
    });
}

From source file:info.jtrac.repository.HibernateJtracDao.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/* ww  w . j  a  va2  s.  c  o  m*/
@Transactional(propagation = Propagation.SUPPORTS)
public List<Item> findAllItems(final int firstResult, final int batchSize) {
    entityManager.clear();
    Session session = getSession();
    Criteria criteria = session.createCriteria(Item.class);
    criteria.setCacheMode(CacheMode.IGNORE);
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    criteria.setFetchMode("history", FetchMode.JOIN);
    criteria.add(Restrictions.ge("id", (long) firstResult));
    criteria.add(Restrictions.lt("id", (long) firstResult + batchSize));
    return criteria.list();
}

From source file:io.github.jonestimd.finance.dao.hibernate.HibernateDao.java

License:Open Source License

protected <T extends ENTITY> Criteria buildGetAllCriteria(Class<T> criteriaClass, String... associations) {
    Criteria criteria = getSession().createCriteria(criteriaClass);
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    criteria.setCacheable(true);/*from  w w w  . ja v  a2  s .co  m*/
    for (String path : associations) {
        criteria.setFetchMode(path, FetchMode.JOIN);
    }
    return criteria;
}

From source file:is.merkor.statistics.hibernate.daos.LexicalItemDAO.java

/**
 * Finds all <code>LexicalItem</code>s matching <code>lemma</code>.
 * Note: the regex has to be made postgresql conform before calling
 * this method - replace '(.)*' by '%' and '(.)?' by '_'.
 * At the moment only simple wild card match is supported, no tests
 * have been made for example with 'or' or character sets.
 * // ww w .  ja v  a 2s .c om
 * Fetches {@link ClusterItem}s for each found item.
 * 
 * @throws {@link IllegalArgumentException} if regex is {@code null}
 * @param regex the regex to match
 * @return a List of LexicalItems matching lemma, an empty List if nothing is found
 */

public List<LexicalItem> findItemsMatching(String regex) {
    if (null == regex) {
        throw new IllegalArgumentException("parameter 'regex' must not be null!");
    }
    SessionFactory factory = HibernateUtil.getSessionFactory();
    Session session = factory.openSession();
    try {
        Criteria criteria = session.createCriteria(LexicalItem.class)
                .setFetchMode("clusterItems", FetchMode.JOIN).add(Restrictions.ilike("lemma", regex));
        List<LexicalItem> result = criteria.list();
        return result;
    } finally {
        session.close();
    }
}

From source file:ispyb.server.common.daos.shipping.Dewar3DAOBean.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from  www  . ja  va  2s.  c  o  m
public List<Dewar3VO> findFiltered(Integer proposalId, Integer shippingId, String type, String code,
        String barCode, String comments, Date date1, Date date2, String dewarStatus, String storageLocation,
        Integer dewarId, Integer experimentId, boolean fetchSession, boolean fetchDewarHistory,
        boolean fetchContainer) {
    try {
        Session session = (Session) this.entityManager.getDelegate();
        Criteria criteria = session.createCriteria(Dewar3VO.class);

        if (dewarId != null) {
            criteria.add(Restrictions.eq("dewarId", dewarId));
        }

        if (experimentId != null) {
            Criteria sessionCriteria = criteria.createCriteria("sessionVO");
            sessionCriteria.add(Restrictions.eq("sessionId", experimentId));
        }
        if (proposalId != null || shippingId != null || (date1 != null) || (date2 != null)) {

            Criteria shippingCriteria = criteria.createCriteria("shippingVO");
            if (proposalId != null) {

                Criteria proposalCriteria = shippingCriteria.createCriteria("proposalVO");
                proposalCriteria.add(Restrictions.eq("proposalId", proposalId));
            }

            if (shippingId != null) {
                shippingCriteria.add(Restrictions.eq("shippingId", shippingId));
            }

            if ((date1 != null) || (date2 != null)) {
                if (date1 != null)
                    shippingCriteria.add(Restrictions.ge("creationDate", date1));
                if (date2 != null)
                    shippingCriteria.add(Restrictions.le("creationDate", date2));
            }
        }

        if (type != null && !type.isEmpty()) {
            criteria.add(Restrictions.like("type", type));
        }

        if (code != null && !code.isEmpty()) {
            criteria.add(Restrictions.like("code", code));
        }

        if (barCode != null && !barCode.isEmpty()) {
            criteria.add(Restrictions.like("barCode", barCode));
        }

        if (comments != null && !comments.isEmpty()) {
            criteria.add(Restrictions.like("comments", comments));
        }

        if (dewarStatus != null && !dewarStatus.isEmpty()) {
            criteria.add(Restrictions.like("dewarStatus", dewarStatus));
        }

        if (storageLocation != null && !storageLocation.isEmpty()) {
            criteria.add(Restrictions.like("storageLocation", storageLocation));
        }

        if (fetchSession) {
            criteria.setFetchMode("sessionVO", FetchMode.JOIN);
            criteria.createCriteria("sessionVO");
        }

        if (fetchDewarHistory) {
            criteria.setFetchMode("dewarTransportHistoryVOs", FetchMode.JOIN);
        }

        if (fetchContainer) {
            criteria.setFetchMode("containerVOs", FetchMode.JOIN);
        }

        criteria.addOrder(Order.desc("dewarId"));

        return criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
    } catch (Exception exp) {
        exp.printStackTrace();
        return null;
    }

}