Example usage for org.hibernate Session createNativeQuery

List of usage examples for org.hibernate Session createNativeQuery

Introduction

In this page you can find the example usage for org.hibernate Session createNativeQuery.

Prototype

NativeQuery createNativeQuery(String sqlString);

Source Link

Document

Create a NativeQuery instance for the given native (SQL) query

Usage

From source file:de.tudarmstadt.ukp.wikipedia.api.Category.java

License:Apache License

/**
 * @see de.tudarmstadt.ukp.wikipedia.api.Category#Category(Wikipedia, String)
 *//*from   ww w  .  j  a  v a2 s . com*/
private void createCategory(Title title) throws WikiPageNotFoundException {
    String name = title.getWikiStyleTitle();
    Session session = this.wiki.__getHibernateSession();
    session.beginTransaction();

    Object returnValue;
    returnValue = session
            .createNativeQuery("select cat.pageId from Category as cat where cat.name = :name COLLATE utf8_bin")
            .setParameter("name", name, StringType.INSTANCE).uniqueResult();
    session.getTransaction().commit();

    // if there is no category with this name, the hibernateCategory is null
    if (returnValue == null) {
        hibernateCategory = null;
        throw new WikiPageNotFoundException("No category with name " + name + " was found.");
    } else {
        // now cast it into an integer
        int pageID = (Integer) returnValue;
        createCategory(pageID);
    }
}

From source file:de.tudarmstadt.ukp.wikipedia.api.Category.java

License:Apache License

/**
 * This is a more efficient shortcut for writing "getParents().size()", as that would require to load all the parents first.
 * @return The number of parents of this category.
 *//*www.j  a v a  2  s .co m*/
public int getNumberOfParents() {
    BigInteger nrOfInlinks = new BigInteger("0");

    long id = this.__getId();
    Session session = this.wiki.__getHibernateSession();
    session.beginTransaction();
    Object returnValue = session.createNativeQuery("select count(inLinks) from category_inlinks where id = :id")
            .setParameter("id", id, LongType.INSTANCE).uniqueResult();
    session.getTransaction().commit();

    if (returnValue != null) {
        nrOfInlinks = (BigInteger) returnValue;
    }
    return nrOfInlinks.intValue();
}

From source file:de.tudarmstadt.ukp.wikipedia.api.Category.java

License:Apache License

/**
 * This is a more efficient shortcut for writing "getChildren().size()", as that would require to load all the children first.
 * @return The number of children of this category.
 *///from  www.j av a  2s .  co m
public int getNumberOfChildren() {
    BigInteger nrOfOutlinks = new BigInteger("0");

    long id = this.__getId();
    Session session = this.wiki.__getHibernateSession();
    session.beginTransaction();
    Object returnValue = session
            .createNativeQuery("select count(outLinks) from category_outlinks where id = :id")
            .setParameter("id", id, LongType.INSTANCE).uniqueResult();
    session.getTransaction().commit();

    if (returnValue != null) {
        nrOfOutlinks = (BigInteger) returnValue;
    }
    return nrOfOutlinks.intValue();
}

From source file:de.tudarmstadt.ukp.wikipedia.api.Category.java

License:Apache License

/**
 * This is a more efficient shortcut for writing "getPages().size()", as that would require to load all the pages first.
 * @return The number of pages./*  w  ww.  j av  a  2s . c o m*/
 */
public int getNumberOfPages() throws WikiApiException {
    BigInteger nrOfPages = new BigInteger("0");

    long id = this.__getId();
    Session session = this.wiki.__getHibernateSession();
    session.beginTransaction();
    Object returnValue = session.createNativeQuery("select count(pages) from category_pages where id = :id")
            .setParameter("id", id, LongType.INSTANCE).uniqueResult();
    session.getTransaction().commit();

    if (returnValue != null) {
        nrOfPages = (BigInteger) returnValue;
    }
    return nrOfPages.intValue();
}

From source file:de.tudarmstadt.ukp.wikipedia.api.Page.java

License:Apache License

/**
 * CAUTION: Only returns 1 result, even if several results are possible.
 *
 * @param pTitle//from www  . ja va2s. c o  m
 * @throws WikiApiException
 */
private void fetchByTitle(Title pTitle, boolean useExactTitle) throws WikiApiException {
    String searchString = pTitle.getPlainTitle();
    if (!useExactTitle) {
        searchString = pTitle.getWikiStyleTitle();
    }

    Session session;
    session = this.wiki.__getHibernateSession();
    session.beginTransaction();
    Integer pageId = (Integer) session
            .createNativeQuery("select pml.pageID from PageMapLine as pml where pml.name = :pagetitle LIMIT 1")
            .setParameter("pagetitle", searchString, StringType.INSTANCE).uniqueResult();
    session.getTransaction().commit();

    if (pageId == null) {
        throw new WikiPageNotFoundException("No page with name " + searchString + " was found.");
    }
    fetchByPageId(pageId);
    if (!this.isRedirect && searchString != null && !searchString.equals(getTitle().getRawTitleText())) {
        if (this.isRedirect) {
            //in case we already tried to re-retrieve the discussion page unsuccessfully,
            //we have to give up here or we end up in an infinite loop.

            //reasons for this happening might be several entries in PageMapLine with the same name but different upper/lower case variants
            //if the database does not allow case sensitive queries, then the API will always retrieve only the first result and if this is a redirect to a different writing variant, we are stuck in a loop.
            //To fix this, either a case sensitive collation should be used or the API should be able to deal with set valued results and pick the correct one from the set.
            //For now, we gracefully return without retrieving the Talk page for this article and throw an appropriate excption.
            throw new WikiPageNotFoundException("No discussion page with name " + searchString
                    + " could be retrieved. This is most likely due to multiple writing variants of the same page in the database");
        } else {
            this.isRedirect = true;
            /*
             * WORKAROUND
             * in our page is a redirect to a discussion page, we might not retrieve the target discussion page as expected but rather the article associated with the target discussion page
             * we check this here and re-retrieve the correct page.
             * this error should be avoided by keeping the namespace information in the database
             * This fix has been provided by Shiri Dori-Hacohen and is discussed in the Google Group under https://groups.google.com/forum/#!topic/jwpl/2nlr55yp87I/discussion
             */
            if (searchString.startsWith(DISCUSSION_PREFIX)
                    && !getTitle().getRawTitleText().startsWith(DISCUSSION_PREFIX)) {
                try {
                    fetchByTitle(new Title(DISCUSSION_PREFIX + getTitle().getRawTitleText()), useExactTitle);
                } catch (WikiPageNotFoundException e) {
                    throw new WikiPageNotFoundException("No page with name " + DISCUSSION_PREFIX
                            + getTitle().getRawTitleText() + " was found.");
                }
            }
        }
    }
}

From source file:de.tudarmstadt.ukp.wikipedia.api.Page.java

License:Apache License

/**
 * This is a more efficient shortcut for writing "getCategories().size()", as that would require
 * to load all the categories first./*from w  w w  . j  ava2s  . c om*/
 *
 * @return The number of categories.
 */
public int getNumberOfCategories() {
    BigInteger nrOfCategories = new BigInteger("0");

    long id = __getId();
    Session session = wiki.__getHibernateSession();
    session.beginTransaction();
    Object returnValue = session
            .createNativeQuery("select count(pages) from page_categories where id = :pageid")
            .setParameter("pageid", id, LongType.INSTANCE).uniqueResult();
    session.getTransaction().commit();

    if (returnValue != null) {
        nrOfCategories = (BigInteger) returnValue;
    }
    return nrOfCategories.intValue();
}

From source file:de.tudarmstadt.ukp.wikipedia.api.Page.java

License:Apache License

/**
 * This is a more efficient shortcut for writing "getInlinks().size()", as that would require to
 * load all the inlinks first.//from   w w w.  j  a va  2s  .  co m
 *
 * @return The number of inlinks.
 */
public int getNumberOfInlinks() {
    BigInteger nrOfInlinks = new BigInteger("0");

    long id = __getId();
    Session session = wiki.__getHibernateSession();
    session.beginTransaction();
    Object returnValue = session
            .createNativeQuery("select count(pi.inLinks) from page_inlinks as pi where pi.id = :piid")
            .setParameter("piid", id, LongType.INSTANCE).uniqueResult();
    session.getTransaction().commit();

    if (returnValue != null) {
        nrOfInlinks = (BigInteger) returnValue;
    }
    return nrOfInlinks.intValue();
}

From source file:de.tudarmstadt.ukp.wikipedia.api.Page.java

License:Apache License

/**
 * This is a more efficient shortcut for writing "getOutlinks().size()", as that would require
 * to load all the outlinks first./*  w ww.  j a v a  2  s  .co m*/
 *
 * @return The number of outlinks.
 */
public int getNumberOfOutlinks() {
    BigInteger nrOfOutlinks = new BigInteger("0");

    long id = __getId();
    Session session = wiki.__getHibernateSession();
    session.beginTransaction();
    Object returnValue = session.createNativeQuery("select count(outLinks) from page_outlinks where id = :id")
            .setParameter("id", id, LongType.INSTANCE).uniqueResult();
    session.getTransaction().commit();

    if (returnValue != null) {
        nrOfOutlinks = (BigInteger) returnValue;
    }
    return nrOfOutlinks.intValue();
}

From source file:de.tudarmstadt.ukp.wikipedia.api.Wikipedia.java

License:Apache License

/**
 * Gets the title for a given pageId./* w w  w . j  a  va  2  s  . c om*/
 *
 * @param pageId The id of the page.
 * @return The title for the given pageId.
 * @throws WikiApiException
 */
public Title getTitle(int pageId) throws WikiApiException {
    Session session = this.__getHibernateSession();
    session.beginTransaction();
    Object returnValue = session.createNativeQuery("select p.name from PageMapLine as p where p.id = :pId")
            .setParameter("pId", pageId, IntegerType.INSTANCE).uniqueResult();
    session.getTransaction().commit();

    String title = (String) returnValue;
    if (title == null) {
        throw new WikiPageNotFoundException();
    }
    return new Title(title);
}

From source file:de.tudarmstadt.ukp.wikipedia.api.Wikipedia.java

License:Apache License

/**
 * Tests, whether a page or redirect with the given title exists.
 * Trying to retrieve a page that does not exist in Wikipedia throws an exception.
 * You may catch the exception or use this test, depending on your task.
 * @param title The title of the page.//from w  w  w. j  av  a 2 s .co  m
 * @return True, if a page or redirect with that title exits, false otherwise.
 */
public boolean existsPage(String title) {

    if (title == null || title.length() == 0) {
        return false;
    }

    Title t;
    try {
        t = new Title(title);
    } catch (WikiTitleParsingException e) {
        return false;
    }
    String encodedTitle = t.getWikiStyleTitle();

    Session session = this.__getHibernateSession();
    session.beginTransaction();
    Object returnValue = session
            .createNativeQuery("select p.id from PageMapLine as p where p.name = :pName COLLATE utf8_bin")
            .setParameter("pName", encodedTitle, StringType.INSTANCE).uniqueResult();
    session.getTransaction().commit();

    return returnValue != null;
}