Example usage for org.hibernate SessionFactory getCurrentSession

List of usage examples for org.hibernate SessionFactory getCurrentSession

Introduction

In this page you can find the example usage for org.hibernate SessionFactory getCurrentSession.

Prototype

Session getCurrentSession() throws HibernateException;

Source Link

Document

Obtains the current session.

Usage

From source file:edu.duke.cabig.c3pr.infrastructure.interceptor.NotificationInterceptor.java

License:BSD License

/**
 * Gets the planned notifications for the list of sites that are passed in.
 * This method access the db using a new session from the hibernate session factory.
 * /*from   www  .j av  a 2 s . c om*/
 * @param hcsList the hcs list
 * @return the planned notifications
 */
public List<PlannedNotification> getPlannedNotifications(List<HealthcareSite> hcsList) {
    List<PlannedNotification> result;
    List<String> nciCodeList = new ArrayList<String>();
    for (HealthcareSite hcs : hcsList) {
        if (hcs != null) {
            nciCodeList.add(hcs.getPrimaryIdentifier());
        }
    }

    SessionFactory sessionFactory = (SessionFactory) applicationContext.getBean("sessionFactory");
    Session session = sessionFactory.openSession(sessionFactory.getCurrentSession().connection());
    session.setFlushMode(FlushMode.MANUAL);
    result = new ArrayList<PlannedNotification>();
    try {
        Query query = session.createQuery(
                "select p from PlannedNotification p, HealthcareSite o, Identifier i where p = any elements(o.plannedNotificationsInternal) and "
                        + "i = any elements(o.identifiersAssignedToOrganization) and i.primaryIndicator = 'true' and "
                        + "i.value in (:nciCodeList)")
                .setParameterList("nciCodeList", nciCodeList);

        result = query.list();
    } catch (DataAccessResourceFailureException e) {
        log.error(e.getMessage());
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (HibernateException e) {
        log.error(e.getMessage());
    } catch (Exception e) {
        log.error(e.getMessage());
    } finally {
        session.close();
    }
    return result;
}

From source file:edu.duke.cabig.c3pr.infrastructure.interceptor.NotificationInterceptor.java

License:BSD License

/**
 * Gets the planned notifications for the list of sites that are passed in.
 * This method access the db using a new session from the hibernate session factory.
 * //  ww w  .  ja  va2 s.c om
 * @param hcsList the hcs list
 * @return the planned notifications
 */
public List<PlannedNotification> getPlannedNotificationsForUpdateMasterSubject() {
    List<PlannedNotification> result;

    SessionFactory sessionFactory = (SessionFactory) applicationContext.getBean("sessionFactory");
    Session session = sessionFactory.openSession(sessionFactory.getCurrentSession().connection());
    session.setFlushMode(FlushMode.MANUAL);
    result = new ArrayList<PlannedNotification>();
    try {
        Query query = session.createQuery("from PlannedNotification p where p.eventName = :var")
                .setString("var", NotificationEventTypeEnum.MASTER_SUBJECT_UPDATED_EVENT.toString());

        result = query.list();
    } catch (DataAccessResourceFailureException e) {
        log.error(e.getMessage());
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (HibernateException e) {
        log.error(e.getMessage());
    } catch (Exception e) {
        log.error(e.getMessage());
    } finally {
        session.close();
    }
    return result;
}

From source file:edu.duke.cabig.c3pr.infrastructure.interceptor.NotificationInterceptor.java

License:BSD License

public List<PlannedNotification> getPlannedNotificationsForCorrespondence() {
    List<PlannedNotification> result;

    SessionFactory sessionFactory = (SessionFactory) applicationContext.getBean("sessionFactory");
    Session session = sessionFactory.openSession(sessionFactory.getCurrentSession().connection());
    session.setFlushMode(FlushMode.MANUAL);
    result = new ArrayList<PlannedNotification>();
    try {/*from  ww  w.j  av  a2  s  .  c om*/
        Query query = session.createQuery(
                "from PlannedNotification p left join fetch p.userBasedRecipientInternal where p.eventName = :var")
                .setString("var", NotificationEventTypeEnum.CORRESPONDENCE_CREATED_OR_UPDATED_EVENT.toString());

        result = query.list().subList(0, 1);
    } catch (DataAccessResourceFailureException e) {
        log.error(e.getMessage());
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (HibernateException e) {
        log.error(e.getMessage());
    } catch (Exception e) {
        log.error(e.getMessage());
    } finally {
        session.close();
    }
    return result;
}

From source file:edu.harvard.i2b2.crc.util.HibernateUtil.java

License:Open Source License

/**
 * Function to fetch session via jboss hibernate mbean
 * Enables filter condition for delete_flag in query
 * tables/*www  . ja  v  a 2  s  .com*/
 * @return Session
 */
public static Session getSession() {
    Session session = null;

    InitialContext ctx;
    SessionFactory factory;

    try {
        ctx = new InitialContext();
        factory = (SessionFactory) ctx.lookup(DATASOURCE_JNDI_NAME);
        // session = factory.openSession();
        session = factory.getCurrentSession();
        session.enableFilter("deleteInstanceFlagFilter").setParameter("deleteFlagFilterParam", "N");
        session.enableFilter("deleteMasterFlagFilter").setParameter("deleteFlagFilterParam", "N");
        session.enableFilter("deleteResultInstanceFlagFilter").setParameter("deleteFlagFilterParam", "N");
    } catch (NamingException e) {
        log.error("DB Session jndi lookup[" + DATASOURCE_JNDI_NAME + "] failed", e);
        throw new ExceptionInInitializerError(e);
    }

    return session;
}

From source file:edu.mum.computersecurity.reposatory.impl.DBAccessDAOImpl.java

@Override
public <T> void create(T t) {
    SessionFactory.getCurrentSession().persist(t);
}

From source file:edu.mum.computersecurity.reposatory.impl.DBAccessDAOImpl.java

@Override
public <T> List<T> getList(T t) {
    //List<Sprint>sprints = new ArrayList<Sprint>();
    String query = "select * from mumscrum.sprint";

    SessionFactory.getCurrentSession().createQuery(query);//"from");
    return null;//from w  w  w . j a v a  2 s. co  m
}

From source file:edu.psu.iam.cpr.core.database.Database.java

License:Apache License

/**
 * openSession routine attempts to obtain a database connection from the ESB connection
 * pool.//from  ww w  .j a  va  2 s.  c  om
 * @param sessionFactory hibernate session factory.
 */
public void openSession(final SessionFactory sessionFactory) {

    session = sessionFactory.getCurrentSession();
    session.beginTransaction();
}

From source file:edu.rmit.eres.seaports.database.CategoriesLoader.java

License:Open Source License

/**
 * Main method used to load CSIRO and CMAR data only.
 * On an existing database, this may duplicate data.
 * @param args: no parameters// www.j  av a 2s. co  m
 */
public static void main(String[] args) {
    AnnotationConfiguration config = new AnnotationConfiguration();
    config.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
    config.configure("hibernate.cfg.xml");
    new SchemaExport(config).create(true, true);

    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();

    LoadCategories(session);

    session.getTransaction().commit();
}

From source file:edu.rmit.eres.seaports.database.CurrentClimateVulnerabilityDataSourceLoader.java

License:Open Source License

/**
 * Main method used to load  the CSIRO Data Source only.
 * On an existing database, this may duplicate data.
 * @param args: no parameters/*from   ww w.j a v a 2s.c  o  m*/
 */
public static void main(String[] args) {
    AnnotationConfiguration config = new AnnotationConfiguration();
    config.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
    config.configure("hibernate.cfg.xml");
    new SchemaExport(config).create(true, true);

    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();

    LoadCurrentClimateVulnerabilityDataSource(session);

    session.getTransaction().commit();
}

From source file:edu.rmit.eres.seaports.database.DatabaseLoader.java

License:Open Source License

/**
 * Main method loading all the initial data in the database using Hibernate
 * Running this is equivalent to importing the 'seaports_dump.sql' SQL script 
 * in an empty 'seaports' database, minus the engineering model examples.
 * @param args: no parameters/*from www.  j a  va 2 s  . c o  m*/
 */
public static void main(String[] args) {
    AnnotationConfiguration config = new AnnotationConfiguration();
    config.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
    config.configure("hibernate.cfg.xml");
    new SchemaExport(config).create(true, true);

    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();

    // Initial user accounts
    User user = new User("user", DEFAULT_PASSWORD, true, true, UserLoginService.ROLE_USER, "email@company.com",
            "User", "User");
    session.save(user);
    User admin = new User("admin", DEFAULT_PASSWORD, true, true, UserLoginService.ROLE_ADMINISTRATOR,
            "email@company.com", "Admin", "Admin");
    session.save(admin);

    // Loads the regions and seaports
    RegionsLoader.LoadRegions(session);

    // Loads the element categories
    CategoriesLoader.LoadCategories(session);

    // Loads the display types available
    DisplayTypesLoader.LoadDisplayTypes(session);

    // Loads the various data sources
    ObservedTrendDataSourceLoader.LoadObservedTrendDataSource(session);
    ObservedExtremeDataSourceLoader.LoadObservedExtremeDataSource(session);
    ProjectedClimateChangeDataSourceLoader.LoadProjectedClimateChangeDataSource(session);
    ProjectedClimateExtremeDataSourceLoader.LoadProjectedClimateExtremeDataSource(session);
    PastClimateImpactDescriptionDataSourceLoader.LoadPastClimateImpactDescriptionDataSource(session);
    CurrentClimateVulnerabilityDataSourceLoader.LoadCurrentClimateVulnerabilityDataSource(session);
    FutureClimateRiskDataSourceLoader.LoadFutureClimateRiskDataSource(session);
    TradeDataSourceLoader.LoadTradeDataSource(session);
    DemographicsDataSourceLoader.LoadDemographicsDataSource(session);

    session.getTransaction().commit();

    System.out.println("Created database schema with initial data");
    System.out.println("DONE !");
}