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:com.farmafene.aurius.dao.hibernate.dic.TestSeq.java

License:Open Source License

public static void main(String... args) {
    Properties props = new Properties();
    props.put("org.hibernate.dialect.MySQLDialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
    props.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
    props.put("hibernate.connection.url", "jdbc:mysql://localhost/dic");
    props.put("hibernate.connection.username", "system");
    props.put("hibernate.connection.password", "manager");
    props.put("hibernate.current_session_context_class",
            "thread"/* "org.hibernate.context.ThreadLocalSessionContext" */);
    props.put("hibernate.show_sql", "true");
    AnnotationConfiguration conf = new AnnotationConfiguration();
    conf.addProperties(props);//from   ww w  . j av  a2s .  c o m
    conf.addAnnotatedClass(DiccionarioDesc.class);
    conf.addAnnotatedClass(UserUsr.class);
    conf.addAnnotatedClass(RoleRol.class);
    conf.addAnnotatedClass(GrupoGrp.class);
    conf.addAnnotatedClass(Test.class);
    conf.addAnnotatedClass(TestUUIDHex.class);
    SessionFactory sf = conf.buildSessionFactory();
    Session sess = sf.getCurrentSession();
    DiccionarioDesc o = new DiccionarioDesc();
    o.setDescripcion("pruebas x");
    Transaction tx = sess.beginTransaction();
    //      UserUsr user = (UserUsr) sess.get(UserUsr.class, new BigDecimal("1"));
    //      Set<RoleRol> roles = user.getRoles();
    //      if (roles != null) {
    //         System.out.println("--> Hay roles");
    //         for (RoleRol r : roles) {
    //            System.out.println(r);
    //         }
    //      }
    //      Set<GrupoGrp> grupos = user.getGrupos();
    //      if (grupos != null) {
    //         System.out.println("--> Hay grupos");
    //         for (GrupoGrp r : grupos) {
    //            System.out.println(r);
    //            System.out.println(r.getGrupos());
    //
    //         }
    //      }
    TestUUIDHex test = new TestUUIDHex();
    sess.save(test);
    tx.commit();
}

From source file:com.flipkart.flux.guice.interceptor.TransactionInterceptor.java

License:Apache License

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {

    Transaction transaction = null;//from   ww  w. ja va 2 s  . c  om
    Session session = null;
    SessionFactoryContext context = contextProvider.get();

    try {
        SessionFactory currentSessionFactory = context.getSessionFactory();
        if (currentSessionFactory != null) {
            session = currentSessionFactory.getCurrentSession();
        }
    } catch (HibernateException e) {
    }

    if (session == null) {
        //start a new session and transaction if current session is null
        //get DataSourceType first
        SelectDataSource selectedDS = invocation.getMethod().getAnnotation(SelectDataSource.class);
        if (selectedDS == null) {
            context.useDefault();
        } else {
            context.useSessionFactory(selectedDS.value());
        }
        session = context.getSessionFactory().openSession();
        ManagedSessionContext.bind(session);
        transaction = session.getTransaction();
        transaction.begin();
    }

    try {

        Object result = invocation.proceed();

        if (transaction != null) {
            transaction.commit();
        }

        return result;

    } catch (Exception e) {
        if (transaction != null) {
            transaction.rollback();
        }
        throw e;
    } finally {
        if (transaction != null && session != null) {
            ManagedSessionContext.unbind(context.getSessionFactory());
            session.close();
            context.clear();
        }
    }
}

From source file:com.flipkart.flux.interceptor.TransactionInterceptorTest.java

License:Apache License

private void prepareInteractions(SessionFactory sf) {
    Session mockedSession1 = mock(Session.class);
    Transaction mockedTransaction1 = mock(Transaction.class);

    when(sf.openSession()).thenReturn(mockedSession1);
    when(sf.getCurrentSession()).thenReturn(null, mockedSession1);
    when(mockedSession1.getTransaction()).thenReturn(mockedTransaction1);
}

From source file:com.flipkart.flux.rule.DbClearRule.java

License:Apache License

/** Clears all given tables which are mentioned using the given sessionFactory*/
private void clearDb(Class[] tables, SessionFactory sessionFactory) {
    Session session = sessionFactory.openSession();
    ManagedSessionContext.bind(session);
    Transaction tx = session.beginTransaction();
    try {//from  w  w w  .  java2 s.  c om
        sessionFactory.getCurrentSession().createSQLQuery("set foreign_key_checks=0").executeUpdate();
        for (Class anEntity : tables) {
            sessionFactory.getCurrentSession().createSQLQuery("delete from " + anEntity.getSimpleName() + "s")
                    .executeUpdate(); //table name is plural form of class name, so appending 's'
        }
        sessionFactory.getCurrentSession().createSQLQuery("set foreign_key_checks=1").executeUpdate();
        tx.commit();
    } catch (Exception e) {
        if (tx != null)
            tx.rollback();
        throw new RuntimeException("Unable to clear tables. Exception: " + e.getMessage(), e);
    } finally {
        if (session != null) {
            ManagedSessionContext.unbind(sessionFactory);
            session.close();
        }
    }
}

From source file:com.github.aint.jblog.model.dao.hibernate.AnonymousMessageHibernateDaoTest.java

License:Open Source License

@BeforeClass
public void beforeClass() {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    anonymousMessageDao = new AnonymousMessageHibernateDao(sessionFactory);
    session = sessionFactory.getCurrentSession();
    session.beginTransaction();//from  www  .  j  a v  a2  s  .  c  o  m
}

From source file:com.github.aint.jblog.model.dao.hibernate.ArticleHibernateDaoTest.java

License:Open Source License

@BeforeClass
public void beforeClass() {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    articleDao = new ArticleHibernateDao(sessionFactory);
    session = sessionFactory.getCurrentSession();
    session.beginTransaction();//from  ww  w .j a v  a2s.  c  o m
}

From source file:com.github.aint.jblog.model.dao.hibernate.CommentHibernateDaoTest.java

License:Open Source License

@BeforeClass
public void beforeClass() {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    commentDao = new CommentHibernateDao(sessionFactory);
    session = sessionFactory.getCurrentSession();
    session.beginTransaction();/* w  w w .ja  v  a2  s .co m*/
}

From source file:com.github.aint.jblog.model.dao.hibernate.HubHibernateDaoTest.java

License:Open Source License

@BeforeClass
public void beforeClass() {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    hubDao = new HubHibernateDao(sessionFactory);
    session = sessionFactory.getCurrentSession();
    session.beginTransaction();/*from w  w w .j  av  a 2s  .com*/
}

From source file:com.github.aint.jblog.model.dao.hibernate.NewsHibernateDaoTest.java

License:Open Source License

@BeforeClass
public void beforeClass() {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    newsDao = new NewsHibernateDao(sessionFactory);
    session = sessionFactory.getCurrentSession();
    session.beginTransaction();//from  w w w  .j av  a 2  s  .  c om
}

From source file:com.github.aint.jblog.model.dao.hibernate.UserHibernateDaoTest.java

License:Open Source License

@BeforeClass
public void beforeClass() {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    userDao = new UserHibernateDao(sessionFactory);
    session = sessionFactory.getCurrentSession();
    session.beginTransaction();//from www.  j  a v  a 2s. co  m
}