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:eu.jangos.manager.controller.AccountService.java

License:Apache License

/**
 * Returns the account corresponding to the given id.
 *
 * @param id The id of the account to be found.
 * @return The account corresponding to the given id. Null if the account is
 * not found.//  ww  w  . ja  v  a2 s. c  om
 */
public Account getAccount(int id) {
    try (Session session = HibernateUtil.getSessionFactory().openSession()) {
        Account account = (Account) session.createCriteria(Account.class).setFetchMode("locale", FetchMode.JOIN)
                .setFetchMode("realm", FetchMode.JOIN)
                .setFetchMode("bannedaccountsForFkBannedaccount", FetchMode.JOIN).add(Restrictions.eq("id", id))
                .uniqueResult();
        return account;
    } catch (HibernateException he) {
        logger.error("There was an error connecting to the database.");
        return null;
    }
}

From source file:eu.jangos.manager.controller.AccountService.java

License:Apache License

/**
 * Returns the account corresponding to the given name. The name must
 * contain only alphanumeric values. Prefer this method to the
 * getAllAccounts if your only criteria is a specific name.
 *
 * @param name The name of the account to be found.
 * @return The account corresponding to the given name. Null if the account
 * is not found./*from  w ww .j  a  v  a  2 s  . c o  m*/
 */
public Account getAccount(String name) {
    if (name == null || name.isEmpty()) {
        logger.error("The account name is null or empty.");
        throw new IllegalArgumentException("The account name is null or empty");
    }

    if (!name.matches("[a-zA-Z0-9]+")) {
        logger.error("The account name must contain only alphanumeric values.");
        throw new IllegalArgumentException("The account name must contain only alphanumeric values");
    }

    try (Session session = HibernateUtil.getSessionFactory().openSession()) {
        Account account = (Account) session.createCriteria(Account.class).setFetchMode("locale", FetchMode.JOIN)
                .setFetchMode("realm", FetchMode.JOIN)
                .setFetchMode("bannedaccountsForFkBannedaccount", FetchMode.JOIN)
                .add(Restrictions.like("name", name)).uniqueResult();
        return account;
    } catch (HibernateException he) {
        logger.error("There was an error connecting to the database.");
        return null;
    }
}

From source file:eu.jangos.manager.controller.RealmService.java

License:Apache License

/**
 * Return the realm information with the corresponding id.
 *
 * @param id The id of the realm to be found.
 * @return A Realm object containing the realm information or null if no
 * matching account is found.//w  ww.  j av a 2s  .  co m
 */
public Realm getRealm(int id) {
    try (Session session = HibernateUtil.getSessionFactory().openSession()) {
        Realm realm = (Realm) session.createCriteria(Realm.class).setFetchMode("realmtimezone", FetchMode.JOIN)
                .setFetchMode("realmtype", FetchMode.JOIN).add(Restrictions.eq("id", id)).uniqueResult();
        return realm;
    } catch (HibernateException he) {
        logger.error("There was an error connecting to the database.");
        return null;
    }
}

From source file:eu.jangos.manager.controller.RealmService.java

License:Apache License

/**
 * Return the list of all realms matching the given criterias.
 *
 * @param name The name of the realm to be found.
 * @param type The type of realm to be found.
 * @param zone The zone of realm to be found.
 * @return A list of realm corresponding to the given filters.
 *//*ww w .  j a  va  2  s  .  c  om*/
public List<Realm> getAllRealms(String name, Realmtype type, Realmtimezone zone) {
    try (Session session = HibernateUtil.getSessionFactory().openSession()) {
        Criteria query = session.createCriteria(Realm.class);

        query.setFetchMode("realmtimezone", FetchMode.JOIN);
        query.setFetchMode("realmtype", FetchMode.JOIN);

        query.add(Restrictions.like("name", name));

        if (!type.getType().equals("ALL")) {
            query.add(Restrictions.eq("realmtype", type));
        }

        if (!zone.getName().equals("ALL")) {
            query.add(Restrictions.eq("realmtimezone", zone));
        }

        return query.list();
    } catch (HibernateException he) {
        logger.error("There was an error connecting to the database.");
        return null;
    }
}

From source file:eu.jangos.manager.controller.RolesService.java

License:Apache License

/**
 * Return the role, as well as its associated commands, with the corresponding ID.
 * @param id The ID of the role to be returned.
 * @return A Roles object corresponding to the ID provided, null if no roles matching this ID has been found.
 *///from  w w w.  j  a v  a  2  s .  co m
public Roles getRoleWithCommands(int id) {
    logger.debug("Returning role with id " + id + " and commands");

    try (Session session = HibernateUtil.getSessionFactory().openSession()) {
        return (Roles) session.createCriteria(Roles.class).setFetchMode("commandses", FetchMode.JOIN)
                .add(Restrictions.like("id", id)).uniqueResult();
    } catch (HibernateException he) {
        logger.debug("The role does not exist.");
        return null;
    }
}

From source file:eu.jangos.realm.controller.auth.AccountService.java

License:Apache License

/**
 * Returns the account corresponding to the given name. The name must
 * contain only alphanumeric values.//from  w  w  w  .  j a v  a  2 s. c om
 *
 * @param name The name of the account to be found.
 * @return The account corresponding to the given name. Null if the account
 * if not found.
 */
public Account getAccount(String name) {
    if (name == null || name.isEmpty()) {
        logger.error("The account name is null or empty.");
        return null;
    }

    if (!name.matches("[a-zA-Z0-9]+")) {
        logger.error("The account name must contain only alphanumeric values.");
        return null;
    }

    try (Session session = HibernateUtil.getAuthSession().openSession()) {
        Account account = (Account) session.createCriteria(Account.class).add(Restrictions.like("name", name))
                .setFetchMode("realmAccounts", FetchMode.JOIN).uniqueResult();
        return account;
    } catch (HibernateException he) {
        logger.error("There was an error connecting to the database.");
        return null;
    }
}

From source file:eu.jangos.realm.controller.auth.RealmService.java

License:Apache License

/**
 * Provides the record for the realm with the given name.
 * @param name The name of the realm to be found.
 * @return A Realm object corresponding to the realm with the provided name. Null if the realm is not found.
 *//*from   ww  w . ja  v  a  2  s .  c  o  m*/
public Realm getRealmByName(String name) {
    if (name == null || name.isEmpty()) {
        logger.error("The parameter name is null or empty");
        return null;
    }

    logger.debug("Returning realm with the name " + name);

    try (Session session = HibernateUtil.getAuthSession().openSession()) {
        return (Realm) session.createCriteria(Realm.class).setFetchMode("realmtype", FetchMode.JOIN)
                .setFetchMode("realmtimezone", FetchMode.JOIN).add(Restrictions.like("name", name))
                .uniqueResult();
    } catch (HibernateException he) {
        logger.debug("Issue while connecting to the database.");
    }
    return null;
}

From source file:eu.jangos.realm.controller.world.StartingEquipmentService.java

License:Apache License

/**
 * Return the list of items for a given race and a given class and a given gender.
 * @param race The race of the character.
 * @param profession The profession of the character.
 * @param gender The gender of the character.
 * @return The list of starting items for this combination.
 *//* w  w  w.j  a v  a  2s  .  c  o  m*/
public List<Startingequipment> getStartingEquipment(Race race, Professions profession, Gender gender) {
    List<Startingequipment> listStartingEquipment = null;
    try (Session session = HibernateUtil.getWorldSession().openSession()) {
        listStartingEquipment = (List<Startingequipment>) session.createCriteria(Startingequipment.class)
                .setFetchMode("item", FetchMode.JOIN).setFetchMode("item.itemsubclass", FetchMode.JOIN)
                .setFetchMode("item.itemclass", FetchMode.JOIN)
                .add(Restrictions.and(Restrictions.eq("race", race), Restrictions.eq("professions", profession),
                        Restrictions.eq("gender", gender)))
                .list();
    } catch (Exception e) {
        e.printStackTrace();
        logger.debug("Exception raised while querying the database.");
    }

    return listStartingEquipment;
}

From source file:fr.mael.microrss.dao.impl.UserArticleDaoImpl.java

License:Open Source License

public List<UserArticle> search(String queryStr, User user, int start, int nb)
        throws ParseException, IOException, InvalidTokenOffsetsException {
    FullTextSession searchSession = Search.getFullTextSession(getSessionFactory().getCurrentSession());
    QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_31,
            new String[] { "article.content", "article.title" }, new StandardAnalyzer(Version.LUCENE_31));
    org.apache.lucene.search.Query query = parser.parse(queryStr);
    FullTextQuery hibQuery = searchSession.createFullTextQuery(query, UserArticle.class);
    Criteria fetchingStrategy = searchSession.createCriteria(UserArticle.class);
    fetchingStrategy.setFetchMode("article.feed", FetchMode.JOIN);
    fetchingStrategy.setFetchMode("userLabels", FetchMode.JOIN);
    fetchingStrategy.add(Property.forName("user").eq(user));
    hibQuery.setCriteriaQuery(fetchingStrategy);
    hibQuery.setFirstResult(start);/*from   w  ww .ja v  a  2s.co  m*/
    hibQuery.setMaxResults(nb);

    QueryScorer scorer = new QueryScorer(query);
    SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("[highlight]", "[/highlight]");
    Highlighter highlighter = new Highlighter(formatter, scorer);
    highlighter.setTextFragmenter(new SimpleSpanFragmenter(scorer, 200));

    List<UserArticle> userArticles = (List<UserArticle>) hibQuery.list();

    for (UserArticle userArticle : userArticles) {
        String highlight = highlighter.getBestFragment(new StandardAnalyzer(Version.LUCENE_32), "content",
                userArticle.getArticle().getContent());
        if (highlight != null) {
            highlight = highlight.replaceAll("\\<.*?>", "").replace("\n", " ");
            userArticle.getArticle().setHighlight(highlight);
        }
    }

    return userArticles;
}

From source file:fr.mael.microrss.dao.impl.UserArticleDaoImpl.java

License:Open Source License

private void manageResults(Class clazz, FullTextSession searchSession) {
    ScrollableResults results = searchSession.createCriteria(clazz).setFetchMode("article", FetchMode.JOIN)
            .setFetchSize(100).scroll(ScrollMode.FORWARD_ONLY);
    int index = 0;
    while (results.next()) {
        index++;//from   ww  w . j  a v  a2s . c om
        searchSession.index(results.get(0));
        if (index % 100 == 0) {
            searchSession.flushToIndexes();
            searchSession.clear();
        }
    }
    searchSession.flushToIndexes();
    searchSession.clear();

}