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.ulcs.dao.CustomerDAO.java

public void insertMember(Customer customer) {
    try {/*from  ww w  . j  a va  2  s  .com*/
        //            Connection con=Teledbc.connector();
        //      String qry="insert into customer(customerName,customerAddress,customerNumber) values(=:customerName,=:customerAddress,=:customerNumber)";
        AnnotationConfiguration config = new AnnotationConfiguration();
        config.addAnnotatedClass(Customer.class);
        config.configure("hibernate.cfg.xml");
        //  new SchemaExport(config).create(true, true);
        SessionFactory factory = config.buildSessionFactory();
        Session session = factory.getCurrentSession();

        session.beginTransaction();
        Customer cus = new Customer();
        cus.setCustomerName(customer.getCustomerName());
        cus.setCustomerAddress(customer.getCustomerAddress());
        cus.setCustomerNumber(customer.getCustomerNumber());
        session.save(cus);
        session.getTransaction().commit();
        //            PreparedStatement pst=con.prepareStatement(qry);
        //            pst.setString(1,customer.getCustomerName());
        //            pst.setString(2,customer.getCustomerAddress());
        //            pst.setString(3,customer.getCustomerNumber());
        //            pst.execute();
        //            con.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.ulcs.dao.CustomerDAO.java

public List<Customer> getMemberInfo() {
    try {/*w  w  w . jav  a  2s  .c o m*/
        List<Customer> customer = new ArrayList<Customer>();
        //            Connection con=Teledbc.connector();
        String qry = "Select * from customer";
        AnnotationConfiguration config = new AnnotationConfiguration();
        config.addAnnotatedClass(Customer.class);
        config.configure("hibernate.cfg.xml");
        SessionFactory factory = config.buildSessionFactory();
        Session session = factory.getCurrentSession();
        session.beginTransaction();

        SQLQuery query = session.createSQLQuery(qry);
        query.addEntity(Customer.class);
        List rs = query.list();
        session.getTransaction().commit();
        return (List<Customer>) rs;
        //            PreparedStatement pst= con.prepareStatement(qry);
        //            ResultSet rs=pst.executeQuery();
        //            
        //            while(rs.next()){
        //                Customer m=new Customer();
        //                m.setCustomerName(rs.getString("customerName"));
        //                m.setCustomerNumber(rs.getString("customerNumber"));
        //                m.setCustomerAddress(rs.getString("customerAddress"));
        //                m.setCustomerID(rs.getInt("CsID"));
        //                customer.add(m);
        //            }
        //            con.close();
        //            return customer;

    }

    catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.ulcs.dao.CustomerDAO.java

public Customer getCustomer(int customerID) {
    try {/*from w  w w.  j  av  a 2 s .  c  o m*/
        //            Connection con=Teledbc.connector();
        String qry = "Select * from customer where customerID=:CsID";
        AnnotationConfiguration config = new AnnotationConfiguration();
        config.addAnnotatedClass(Customer.class);
        config.configure("hibernate.cfg.xml");
        SessionFactory factory = config.buildSessionFactory();
        Session session = factory.getCurrentSession();
        session.beginTransaction();
        SQLQuery query = session.createSQLQuery(qry);
        query.addEntity(Customer.class);
        query.setParameter("CsID", customerID);
        List queryList = query.list();
        session.getTransaction().commit();
        if (queryList != null && queryList.isEmpty()) {
            return null;
        } else {
            return (Customer) queryList.get(0);
        }

        //            PreparedStatement pst= con.prepareStatement(qry);
        //            pst.setInt(1, customerID);
        //            ResultSet rs=pst.executeQuery();
        //            
        //            while(rs.next()){
        //                Customer m=new Customer();
        //                m.setCustomerName(rs.getString("customerName"));
        //                m.setCustomerNumber(rs.getString("customerNumber"));
        //                m.setCustomerAddress(rs.getString("customerAddress"));
        //                m.setCustomerID(rs.getInt("CsID"));
        //                return m;
        //            }
        //            con.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.ulcs.dao.CustomerDAO.java

public void deleteCustomer(int customerID) {
    try {/*from ww  w . j av a  2 s  . co  m*/
        String qry = "delete from customer where customerID=:CsID";
        AnnotationConfiguration config = new AnnotationConfiguration();
        config.addAnnotatedClass(Customer.class);
        config.configure("hibernate.cfg.xml");
        SessionFactory factory = config.buildSessionFactory();
        Session session = factory.getCurrentSession();
        session.beginTransaction();
        SQLQuery query = session.createSQLQuery(qry);
        query.addEntity(Customer.class);
        query.setParameter("CsID", customerID);
        query.executeUpdate();
        session.getTransaction().commit();
        //            Connection con=Teledbc.connector();
        //            String qry="delete from customer where CsID=?";
        //            PreparedStatement pst=con.prepareStatement(qry);
        //            pst.setInt(1,customerID);
        //            pst.executeUpdate();
        //            con.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.ulcs.dao.CustomerDAO.java

public void updateCustomer(Customer customer) {
    try {/*from   w  w w.j  av a 2s .  com*/
        String qry = "update customer set customerName=:customerName ,customerAddress=:customerAddress,customerNumber=:customerNumber where customerID=:CsID";
        AnnotationConfiguration config = new AnnotationConfiguration();
        config.addAnnotatedClass(Customer.class);
        config.configure("hibernate.cfg.xml");
        SessionFactory factory = config.buildSessionFactory();
        Session session = factory.getCurrentSession();
        session.beginTransaction();
        SQLQuery query = session.createSQLQuery(qry);
        query.addEntity(Customer.class);
        query.setParameter("CsID", customer.getCustomerID());
        query.setParameter("customerName", customer.getCustomerName());
        query.setParameter("customerAddress", customer.getCustomerAddress());
        query.setParameter("customerNumber", customer.getCustomerNumber());
        query.executeUpdate();
        session.getTransaction().commit();
        //            Connection con=Teledbc.connector();
        //            String qry="update customer set customerName=? ,customerAddress=?,customerNumber=? where CsID=?";
        //            PreparedStatement pst=con.prepareStatement(qry);
        //            pst.setString(1,customer.getCustomerName());
        //            pst.setString(2,customer.getCustomerAddress());
        //            pst.setString(3,customer.getCustomerNumber());
        //            pst.setInt(4,customer.getCustomerID());
        //            pst.executeUpdate();
        //            con.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.ulcs.dao.UserDAO.java

public User ValidUser(User user) {
    try {//  w  ww. j av a  2  s .c  o m

        String qry = "Select * from user where userName =:userName and userPassword =:userPassword";
        AnnotationConfiguration config = new AnnotationConfiguration();
        config.addAnnotatedClass(User.class);
        config.configure("hibernate.cfg.xml");
        SessionFactory factory = config.buildSessionFactory();
        Session session = factory.getCurrentSession();

        session.beginTransaction();
        SQLQuery query = session.createSQLQuery(qry);
        query.addEntity(User.class);
        query.setParameter("userName", user.getUserName());
        query.setParameter("userPassword", user.getUserPassword());
        List rs = query.list();
        session.getTransaction().commit();
        if (rs != null) {
            return user;
            //    return (User) rs;

        } else
            return null;

    } catch (Exception e) {
        e.printStackTrace();

    }
    return null;
}

From source file:com.vmware.bdd.dal.DAL.java

License:Open Source License

private static SessionFactory buildSessionFactory() {
    try {//from ww  w  .j  a  v  a 2  s .  c  om
        SessionFactory _ssn = new org.hibernate.cfg.Configuration().configure().buildSessionFactory();
        checkIsolationLevel(_ssn.getCurrentSession());
        return _ssn;
    } catch (Throwable ex) {
        throw new ExceptionInInitializerError(ex);
    }
}

From source file:com.vmware.photon.controller.api.common.db.TransactionalInterceptor.java

License:Open Source License

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    SessionFactory sessionFactory = sessionFactoryProvider.get();

    Session session;/*from   ww  w  .j a  va2 s  .com*/
    if (ManagedSessionContext.hasBind(sessionFactory)) {
        session = sessionFactory.getCurrentSession();
    } else {
        session = sessionFactory.openSession();
        ManagedSessionContext.bind(session);
    }

    Transaction transaction = session.getTransaction();
    if (transaction.isActive()) {
        return invocation.proceed();
    }

    Stopwatch stopwatch = Stopwatch.createUnstarted();

    try {
        logger.trace("beginning transaction: {}", transaction);
        stopwatch.start();
        transaction.begin();
        Object result = invocation.proceed();
        transaction.commit();
        stopwatch.stop();
        logger.debug("committed: {}", transaction);
        return result;
    } catch (Throwable t) {
        logger.debug("rolling back: {}", transaction, t);
        transaction.rollback();
        transactionExceptions.mark();
        throw t;
    } finally {
        final long elapsedTime = stopwatch.elapsed(TimeUnit.MILLISECONDS);
        transactions.update(elapsedTime, TimeUnit.MILLISECONDS);
        ManagedSessionContext.unbind(sessionFactory);
        if (session.isOpen()) {
            session.close();
        }
        final long transactionTimeWarningThresholdInMilliseconds = 2000L;
        if (elapsedTime > transactionTimeWarningThresholdInMilliseconds) {
            logger.warn("Transaction {} took {} milliseconds", transaction, elapsedTime);
        }
    }
}

From source file:com.wideplay.warp.persist.hibernate.HibernateWorkManager.java

License:Apache License

public void endWork() {
    if (UnitOfWork.TRANSACTION.equals(unitOfWork))
        throw new IllegalStateException("Cannot use WorkManager with UnitOfWork.TRANSACTION");

    //do nothing if there is no session open
    SessionFactory sessionFactory = sessionFactoryProvider.get();
    if (!ManagedSessionContext.hasBind(sessionFactory))
        return;/*  ww  w .  j  a v  a  2 s . c o  m*/

    //close up session when done
    try {
        sessionFactory.getCurrentSession().close();
    } finally {

        //discard session;
        ManagedSessionContext.unbind(sessionFactory);
    }
}

From source file:com.ycszh.ssh.dao.veh.impl.SlgVehDaoImpl.java

@SuppressWarnings("deprecation")
public TemporaryLicense getTemporaryLicense(String lsh) throws Exception {
    TemporaryLicense tempLicense = new TemporaryLicense();
    Connection conn = null;/*from   w ww  .  j  av  a2 s  . co m*/
    CallableStatement proc = null;
    ResultSet rs = null;
    SessionFactory sf = this.getHibernateTemplate().getSessionFactory();
    Session session = sf.getCurrentSession();
    try {
        conn = session.connection();
        proc = conn.prepareCall("{? = call psbp.get_temporary_license(?)}");
        proc.registerOutParameter(1, OracleTypes.CURSOR);
        proc.setString(2, lsh);
        proc.execute();
        rs = (ResultSet) proc.getObject(1);
        while (rs.next()) {
            tempLicense.setAddress(rs.getString(2)); //??
            tempLicense.setAppointmentDate(rs.getString(3)); //
            tempLicense.setBookNumber(rs.getString(4)); //??
            tempLicense.setCarType(rs.getString(5)); //
            tempLicense.setChassisNumber(rs.getString(6)); //??4?
            tempLicense.setChineseBrand(rs.getString(7)); //?
            tempLicense.setEngineNumber(rs.getString(9)); //??
            tempLicense.setIdNumber(rs.getString(10)); //???
            tempLicense.setName(rs.getString(11)); //??
            tempLicense.setPassengerNumber(rs.getString(12)); //
            tempLicense.setPhoneNumber(rs.getString(13)); //???
            tempLicense.setVehicleType(rs.getString(14)); //
        }
    } catch (Exception re) {
        re.printStackTrace();
        throw re;
    } finally {
        if (rs != null) {
            rs.close();
            rs = null;
        }
        if (proc != null) {
            proc.close();
            proc = null;
        }
        if (conn != null) {
            conn.close();
            conn = null;
        }
    }
    return tempLicense;
}