Example usage for org.hibernate SessionFactory openSession

List of usage examples for org.hibernate SessionFactory openSession

Introduction

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

Prototype

Session openSession() throws HibernateException;

Source Link

Document

Open a Session .

Usage

From source file:br.eb.ime.pfc.filters.AuthenticationFilter.java

License:Open Source License

protected boolean authenticateUser(HttpServletRequest request, HttpServletResponse response, String username,
        String password) {//  w w w. j a va 2s .c om
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    final Session session = sessionFactory.openSession();
    session.beginTransaction();
    ManagedSessionContext.bind(session);
    final UserManager userManager = new UserManager(session);
    if (username.equals("")) {//cannot be empty
        return false;
    }
    String passwordDB = null;
    Set<String> layersIds = new HashSet<>();
    try {
        final User user = userManager.getById(username);
        for (Layer layer : user.getAccessLevel().getLayers()) {
            layersIds.add(layer.getWmsId());
        }
        passwordDB = user.getPassword();
        session.getTransaction().commit();
    } catch (HibernateException | ObjectNotFoundException e) {
        session.getTransaction().rollback();
    } finally {
        session.close();
    }

    if (passwordDB != null && passwordDB.equals(password)) {
        //CACHE LAYERS
        request.getSession().setAttribute("user", username);
        request.getSession().setAttribute("layers", layersIds);
        return true;
    } else {
        return false;
    }
}

From source file:br.eb.ime.pfc.filters.HibernateSessionRequestFilter.java

License:Open Source License

private void doBeforeProcessing(ServletRequest request, ServletResponse response)
        throws IOException, ServletException {
    if (debug) {//from   w  w w.  j  a v a2 s. c o  m
        log("TransactionHandlerFilter:DoBeforeProcessing");
    }

    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    this.session = sessionFactory.openSession();
    this.session.beginTransaction();
    ManagedSessionContext.bind(session);
}

From source file:br.ifmt.dai.curso.GerenciadorEventos.java

public static void salvarEvento(String evento, String data) {
    try {//from  w  w w . j  a v  a2 s  . co  m
        SessionFactory sf = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
        Eventos e = new Eventos();
        e.setDataEvento(data);
        e.setEvento(evento);
        s.saveOrUpdate(e);
        tx.commit();
        s.close();
        System.out.println("Evento incluido");
    } catch (Exception e) {
    }
}

From source file:br.ifmt.dai.curso.GerenciadorEventos.java

public List listaEventos() {
    SessionFactory sf = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
    Session s = sf.openSession();
    s.beginTransaction();//  ww w  .  j  a v  a2  s  .c  o m
    List lista = s.createQuery("from Eventos").list();
    s.getTransaction().commit();
    return lista;
}

From source file:br.ufmt.DadosAmbientais.AppTest.java

public static void main(String[] args) {
    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();/*  w w w .  ja  v  a 2 s .  c  o m*/

    //AppUser user = new AppUser("admin");
    //session.save(user);

    //session.getTransaction().commit();

    //    session.beginTransaction();
    @SuppressWarnings("unchecked")
    List<User> todosUser = session.createQuery("select name from user_role").list();
    System.out.println(todosUser);
    session.close();
    //teste
}

From source file:business.AccountDB.java

public static Account getAccount(String name) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = null;//w  w w . j a v  a2  s.c  o m
    Account a = null;

    try {
        String qS = "FROM Account a WHERE a.name = :name";
        session = sessionFactory.openSession();
        Query q = session.createQuery(qS);
        q.setParameter("name", name);
        a = (Account) q.uniqueResult();
    } catch (NoResultException e) {
        return null;
    } finally {
        if (session != null && session.isOpen()) {
            session.close();
        }
    }

    return a;
}

From source file:business.AccountDB.java

public static boolean checkAccountName(String name) {
    boolean used = false;
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = null;/*from   www . j  a v a  2  s.c  o  m*/
    Account a;

    try {
        String qS = "FROM Account a WHERE a.name = :name";
        session = sessionFactory.openSession();
        Query q = session.createQuery(qS);
        q.setParameter("name", name);
        a = (Account) q.uniqueResult();
        used = true;
    } catch (NoResultException e) {
        used = false;
    } finally {
        if (session != null && session.isOpen()) {
            session.close();
        }
    }

    return used;
}

From source file:business.AccountDB.java

public static String syncAccount(Account a) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = null;/*from www .  ja va 2  s  .  com*/
    String msg = "";

    try {
        session = sessionFactory.openSession();
        session.beginTransaction();
        session.merge(a);
        session.getTransaction().commit();
        session.flush();
        session.refresh(a);
        msg = "Account Synced!";
    } catch (HibernateException e) {
        msg = "Error Syncing Account: " + e.getMessage();
        session.getTransaction().rollback();
    } finally {
        session.close();
    }

    return msg;
}

From source file:business.AccountDB.java

public static String addNewAccount(Account a) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = null;/*from   ww  w .  j a  va 2s  . c  om*/
    String msg = "";

    try {
        session = sessionFactory.openSession();
        session.beginTransaction();
        session.persist(a);
        session.getTransaction().commit();
        session.flush();
        session.refresh(a);
        msg = "Account Created!";
    } catch (HibernateException e) {
        msg = "Error Creating Account: " + e.getMessage();
        session.getTransaction().rollback();
    } finally {
        if (session != null && session.isOpen()) {
            session.close();
        }
    }

    return msg;
}

From source file:business.ChampionDB.java

public static Champion getChampionByID(int cid) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = null;/*from  w  ww.ja v  a 2 s  .c o m*/
    Champion c = null;

    try {
        String qS = "FROM Champion c WHERE c.id = :cid";
        session = sessionFactory.openSession();
        Query q = session.createQuery(qS);
        q.setParameter("cid", cid);
        c = (Champion) q.uniqueResult();
    } catch (HibernateException e) {
        c = null;
    } finally {
        if (session != null && session.isOpen()) {
            session.close();
        }
    }
    return c;
}