Example usage for org.hibernate Session clear

List of usage examples for org.hibernate Session clear

Introduction

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

Prototype

void clear();

Source Link

Document

Completely clear the session.

Usage

From source file:eu.cloudscale.showcase.generate.AGenerate.java

License:Open Source License

@Override
public void populateItemTable() {
    String I_TITLE;//ww  w .  j  a  va  2s .c om
    GregorianCalendar cal;
    int I_A_ID;
    java.sql.Date I_PUB_DATE;
    String I_PUBLISHER, I_SUBJECT, I_DESC;
    int I_RELATED1, I_RELATED2, I_RELATED3, I_RELATED4, I_RELATED5;
    String I_THUMBNAIL, I_IMAGE;
    double I_SRP, I_COST, I_RANDOM;
    java.sql.Date I_AVAIL;
    int I_STOCK;
    String I_ISBN;
    int I_PAGE;
    String I_BACKING;
    String I_DIMENSIONS;

    String[] SUBJECTS = { "ARTS", "BIOGRAPHIES", "BUSINESS", "CHILDREN", "COMPUTERS", "COOKING", "HEALTH",
            "HISTORY", "HOME", "HUMOR", "LITERATURE", "MYSTERY", "NON-FICTION", "PARENTING", "POLITICS",
            "REFERENCE", "RELIGION", "ROMANCE", "SELF-HELP", "SCIENCE-NATURE", "SCIENCE-FICTION", "SPORTS",
            "YOUTH", "TRAVEL" };
    int NUM_SUBJECTS = 24;

    String[] BACKINGS = { "HARDBACK", "PAPERBACK", "USED", "AUDIO", "LIMITED-EDITION" };
    int NUM_BACKINGS = 5;

    System.out.println("Populating ITEM table with " + NUM_ITEMS + " items");

    LinkedList<IAuthor> authors = getRandomAuthors(NUM_ITEMS + 10000);
    Random rand = new Random();

    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();

    for (int i = 1; i <= NUM_ITEMS; i++) {
        I_RANDOM = rand.nextDouble();
        int month, day, year, maxday;
        I_TITLE = getRandomAString(14, 60);

        year = getRandomInt(1930, 2000);
        month = getRandomInt(0, 11);
        maxday = 31;
        if (month == 3 | month == 5 | month == 8 | month == 10)
            maxday = 30;
        else if (month == 1)
            maxday = 28;
        day = getRandomInt(1, maxday);
        cal = new GregorianCalendar(year, month, day);
        I_PUB_DATE = new java.sql.Date(cal.getTime().getTime());

        I_PUBLISHER = getRandomAString(14, 60);
        I_SUBJECT = SUBJECTS[getRandomInt(0, NUM_SUBJECTS - 1)];
        I_DESC = getRandomAString(100, 500);

        I_RELATED1 = getRandomInt(1, NUM_ITEMS);
        do {
            I_RELATED2 = getRandomInt(1, NUM_ITEMS);
        } while (I_RELATED2 == I_RELATED1);
        do {
            I_RELATED3 = getRandomInt(1, NUM_ITEMS);
        } while (I_RELATED3 == I_RELATED1 || I_RELATED3 == I_RELATED2);
        do {
            I_RELATED4 = getRandomInt(1, NUM_ITEMS);
        } while (I_RELATED4 == I_RELATED1 || I_RELATED4 == I_RELATED2 || I_RELATED4 == I_RELATED3);
        do {
            I_RELATED5 = getRandomInt(1, NUM_ITEMS);
        } while (I_RELATED5 == I_RELATED1 || I_RELATED5 == I_RELATED2 || I_RELATED5 == I_RELATED3
                || I_RELATED5 == I_RELATED4);

        I_THUMBNAIL = new String("img" + i % 100 + "/thumb_" + i + ".gif");
        I_IMAGE = new String("img" + i % 100 + "/image_" + i + ".gif");
        I_SRP = (double) getRandomInt(100, 99999);
        I_SRP /= 100.0;

        I_COST = I_SRP - ((((double) getRandomInt(0, 50) / 100.0)) * I_SRP);

        cal.add(Calendar.DAY_OF_YEAR, getRandomInt(1, 30));
        I_AVAIL = new java.sql.Date(cal.getTime().getTime());
        I_STOCK = getRandomInt(10, 30);
        I_ISBN = getRandomAString(13);
        I_PAGE = getRandomInt(20, 9999);
        I_BACKING = BACKINGS[getRandomInt(0, NUM_BACKINGS - 1)];
        I_DIMENSIONS = ((double) getRandomInt(1, 9999) / 100.0) + "x" + ((double) getRandomInt(1, 9999) / 100.0)
                + "x" + ((double) getRandomInt(1, 9999) / 100.0);

        IItem item = itemDao.getObject();
        // Set parameter
        // MongoDB doesn't have autoincrement field so we must set ID manually
        if (db instanceof MongoService) {
            item.setIId(i);
        }
        item.setIRandom(I_RANDOM);
        item.setITitle(I_TITLE);
        item.setIPubDate(I_PUB_DATE);
        item.setIPublisher(I_PUBLISHER);
        item.setISubject(I_SUBJECT);
        item.setIDesc(I_DESC);
        item.setIRelated1(I_RELATED1);
        item.setIRelated2(I_RELATED2);
        item.setIRelated3(I_RELATED3);
        item.setIRelated4(I_RELATED4);
        item.setIRelated5(I_RELATED5);
        item.setIThumbnail(I_THUMBNAIL);
        item.setIImage(I_IMAGE);
        item.setISrp(I_SRP);
        item.setICost(I_COST);
        item.setIAvail(I_AVAIL);
        item.setIStock(I_STOCK);
        item.setIIsbn(I_ISBN);
        item.setIPage(String.valueOf(I_PAGE));
        item.setIBacking(I_BACKING);
        item.setIDimension(I_DIMENSIONS);
        item.setAuthor(this.authors.get(getRandomInt(1, this.authors.size() - 1)));

        this.items.add(item);
        session.save(item);
        if (i % 1000 == 0) {
            session.flush();
            session.clear();
            System.out.print((i / 1000) + " ");
        }
        //itemDao.findById( item.getIId() );
    }

    tx.commit();
    session.close();

    itemDao.finish();
    System.out.println("");
}

From source file:fm.last.citrine.dao.TaskRunDAO.java

License:Apache License

/**
 * Finds TaskRuns that belong to a certain Task. Returned List is ordered by TaskRun creation (most recent TaskRun
 * first)./*  w ww . j  a v  a  2  s .  c o m*/
 * 
 * @param taskId The Task ID.
 * @param firstResult The position of the first result.
 * @param maxResults The maximum number of results.
 * @return List of matching TaskRuns.
 */
public List<TaskRun> findByTaskId(final long taskId, final int firstResult, final int maxResults) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
        @Override
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            session.clear(); // need this as sometimes this retrieves older data than what is in DB...
            Query query = session.createQuery("from TaskRun where taskId = :taskId order by id DESC");
            query.setLong("taskId", taskId);
            query.setFirstResult(firstResult);
            query.setMaxResults(maxResults);
            List list = query.list();
            return list;
        }
    });
}

From source file:fr.keyconsulting.oliphant.test.Benchmark.java

License:Open Source License

public void setUp() throws SQLException {
    conn = Utils.getJDBCConnection();//ww w  .  j  a  v a2s  . co  m

    Statement st = conn.createStatement();
    st.executeUpdate("DELETE FROM persistentversionedobject");
    st.close();

    sessionFactory = Utils.getSessionFactory();
    magicSessionFactory = Utils.getMagicSessionFactory();

    Session session = sessionFactory.getCurrentSession();
    Transaction tx = session.beginTransaction();
    for (int i = 0; i < NB_ROWS; i++) {
        PersistentVersionedObject o = new PersistentVersionedObject();
        o.setId(i);
        o.setChampString("valeur string");
        o.setChampLong((long) 1);
        session.save(o);
        if (i % 20 == 0) {
            session.flush();
            session.clear();
        }
    }
    session.flush();
    session.clear();
    tx.commit();
}

From source file:fr.keyconsulting.oliphant.test.unitTests.java

License:Open Source License

@Before
public void setUp() throws SQLException {
    conn = Utils.getJDBCConnection();/* w  w w.  ja  v  a 2 s  . co  m*/
    config = Utils.getConfig();

    Statement st = conn.createStatement();
    for (Iterator i = config.getClassMappings(); i.hasNext();) {
        PersistentClass c = (PersistentClass) i.next();
        st.executeUpdate("DELETE FROM " + c.getTable().getName());
    }
    st.close();

    sessionFactory = Utils.getSessionFactory();
    magicSessionFactory = Utils.getMagicSessionFactory();

    Session session = sessionFactory.getCurrentSession();
    Transaction tx = session.beginTransaction();

    PersistentVersionedObject o = new PersistentVersionedObject();
    o.setChampString("valeur string");
    o.setChampLong((long) 1);
    session.save(o);
    session.flush();
    session.clear();
    tx.commit();
}

From source file:gov.nih.nci.cabig.caaers.dao.CaaersDao.java

License:BSD License

/**
 * Will clear-off the content in the current Hibernate Session
 * @see org.hibernate.Session//from w w w . j  ava 2 s  .  c  om
 */
@Transactional(readOnly = false)
public void clearSession() {

    Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();

    if (log.isDebugEnabled()) {
        log.debug("Entity Count  before clear() : " + session.getStatistics().getEntityCount());
        log.debug("Collection Count before clear() : " + session.getStatistics().getCollectionCount());
    }

    session.clear();

    if (log.isDebugEnabled()) {
        log.debug("Entity Count  after clear() : " + session.getStatistics().getEntityCount());
        log.debug("Collection Count after clear() : " + session.getStatistics().getCollectionCount());
    }

}

From source file:gov.nih.nci.cabig.ctms.acegi.acls.test.ACLTest.java

License:BSD License

public void init() {

    ApplicationContext appCtx = new ClassPathXmlApplicationContext(getConfigLocations());
    HibernateTransactionManager txMgr = (HibernateTransactionManager) appCtx.getBean("transactionManager");

    Session session = SessionFactoryUtils.getSession(txMgr.getSessionFactory(), true);

    if (session != null) {
        session.clear();
    }//from  w w  w.j  a v  a  2  s .  c o  m

    LocalSessionFactoryBean sessionFactoryBean = (LocalSessionFactoryBean) appCtx.getBean("&sessionFactory");

    sessionFactoryBean.createDatabaseSchema();

}

From source file:gov.nih.nci.caintegrator.studyQueryService.germline.BatchFindingsHandler.java

License:BSD License

final protected void populateCurrentResultSet(List findings, List toBePopulated, Session session) {

    /*  1. Remove the first 500 objects and add it to a new HashSet */
    Set toBeSent = getConcreteTypedFindingSet();
    int size = findings.size();
    for (int index = 0; (index < size) && (index <= BATCH_OBJECT_INCREMENT); index++) {
        Object f = findings.remove(0);
        toBeSent.add(f);//from www .  ja  v a 2  s.  c  om
    }

    /* 2. Add results to toBePopulated after making sure it is empty */
    do {
        synchronized (toBePopulated) {
            if (toBePopulated.size() == 0) {
                toBePopulated.add(toBeSent);
                session.clear();
                break;
            }
        }
        try {
            Thread.currentThread().sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace(); // no big deal
        }
    } while (true);
}

From source file:gov.nih.nci.caintegrator.studyQueryService.germline.BatchFindingsHandler.java

License:BSD License

final protected void process(List toBePopulated, Set toBeSent, Session session) {
    /*  Add results to toBePopulated after making sure it is empty */
    while (true) {
        synchronized (toBePopulated) {
            if (toBePopulated.size() == 0) {
                toBePopulated.add(toBeSent);
                session.clear();
                break;
            }//from ww  w.  j  a  v a 2s.  c  om
        }
        try {
            Thread.currentThread().sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace(); // no big deal
        }
    }
    return;
}

From source file:gov.nih.nci.caintegrator.studyQueryService.germline.FindingsHandler.java

License:BSD License

public Collection<? extends Finding> getFindings(FindingCriteriaDTO critDTO, int fromIndex, int toIndex)
        throws Exception {
    Collection<? extends Finding> findings;
    Session session = getSessionFactory().getCurrentSession();
    StringBuffer targetHQL = getTargetFindingHQL();
    findings = executeFindingSetQuery(critDTO, targetHQL, session, fromIndex, toIndex);
    initializeProxies(findings, session);
    session.clear();
    return findings;
}

From source file:gov.nih.nci.caintegrator.studyQueryService.germline.GenotypeFindingsHandler.java

License:BSD License

public Collection<? extends Finding> getFindings(FindingCriteriaDTO critDTO, int fromIndex, int toIndex)
        throws Exception {
    Collection<? extends Finding> findings;
    Session session = getSessionFactory().getCurrentSession();
    findings = getMyFindings(critDTO, session, fromIndex, toIndex);
    initializeProxies(findings, session);
    session.clear();
    return findings;
}