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.onlineshopping.Models.RegisterService.java

public boolean registerUser(User user) {
    boolean result = false;
    if (isUserExists(user)) {
        result = false;//from   w  w  w  .j  a va 2s  .  com
        return result;
    }

    SessionFactory factory = HibernateUtil.getSessionFactory();
    Session session = factory.getCurrentSession();

    Transaction tx = null;
    try {
        tx = session.getTransaction();
        tx.begin();
        session.save(user);
        tx.commit();

        result = true;
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
            result = false;
            e.printStackTrace();
        }
    }
    return result;
}

From source file:com.onlineshopping.Models.RegisterService.java

public boolean isUserExists(User user) {
    boolean result = false;
    SessionFactory factory = HibernateUtil.getSessionFactory();
    Session session = factory.getCurrentSession();
    Transaction tx = null;/*from  ww  w . jav a2 s.com*/
    try {
        tx = session.getTransaction();
        tx.begin();
        Query query = session.createQuery("from User where username='" + user.getUsername() + "'");
        User u = (User) query.uniqueResult();
        tx.commit();
        if (u != null) {
            result = true;
        }
    } catch (Exception ex) {
        if (tx != null) {
            tx.rollback();
        }
    }
    return result;
}

From source file:com.onlineshopping.Models.SearchProductService.java

public List<Product> searchProduct(int manufactureId, int osId, int screenSize, int price, String color) {
    int minScreenSize = 0;
    int maxScreenSize = 0;
    int minPrice = 0;
    int maxPrice = 0;

    //Get screen query size
    switch (screenSize) {
    case 4: {/*from  w  w w  .  j  a v  a 2s  .c  o m*/
        minScreenSize = 0;
        maxScreenSize = 4;
        break;
    }
    case 5: {
        minScreenSize = 4;
        maxScreenSize = 5;
        break;
    }
    case 6: {
        minScreenSize = 5;
        maxScreenSize = 6;
        break;
    }
    case 7: {
        minScreenSize = 6;
        maxScreenSize = 99;
        break;
    }
    default: {
        minScreenSize = 0;
        maxScreenSize = 99;
        break;
    }
    }

    //Get price query range
    switch (price) {
    case 3: {
        minPrice = 0;
        maxPrice = 3000000;
        break;
    }
    case 5: {
        minPrice = 3000000;
        maxPrice = 5000000;
        break;
    }
    case 10: {
        minPrice = 5000000;
        maxPrice = 10000000;
        break;
    }
    case 20: {
        minPrice = 10000000;
        maxPrice = 20000000;
        break;
    }
    case 100: {
        minPrice = 20000000;
        maxPrice = 100000000;
        break;
    }
    default: {
        minPrice = 0;
        maxPrice = 100000000;
        break;
    }
    }

    String strQuery = "from Product where 1=1";
    if (manufactureId != -1) {
        strQuery += " and manufacture='" + manufactureId + "'";
    }
    if (osId != -1) {
        strQuery += " and os='" + osId + "'";
    }
    if (screenSize != -1) {
        strQuery += " and screensize between '" + minScreenSize + "' and '" + maxScreenSize + "'";
    }
    if (price != -1) {
        strQuery += " and price between '" + minPrice + "' and '" + maxPrice + "'";

    }
    if (!color.isEmpty()) {
        strQuery += " and color like '%" + color + "%'";
    }

    List<Product> list = new ArrayList<Product>();
    SessionFactory factory = HibernateUtil.getSessionFactory();
    Session session = factory.getCurrentSession();
    Transaction tx = null;
    try {
        tx = session.getTransaction();
        tx.begin();
        list = session.createQuery(strQuery).list();
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
    }
    return list;
}

From source file:com.projekt.kino.config.QueryObjectDemo.java

public static void main(String[] args) {
    SessionFactory factory = HibernateUtils.getSessionFactory();

    Session session = factory.getCurrentSession();

    try {//from  w w w  . j ava2  s.c om
        Bilet newBilet = new Bilet(1, 'T', 11, 2, 3);
        // All the action with DB via Hibernate
        // must be located in one transaction.
        // Start Transaction.            
        session.getTransaction().begin();
        session.save(newBilet);

        //String sql = "Select * from " + Bilet.class.getName();
        // Create Query object.
        //Query<Bilet> query = session.createQuery(sql);

        // Commit data.
        session.getTransaction().commit();
    } catch (Exception e) {
        // Rollback in case of an error occurred.
        session.getTransaction().rollback();
    }
}

From source file:com.romeikat.datamessie.core.view.ui.dataprovider.CrawlingsOverviewDataProvider.java

License:Open Source License

public CrawlingsOverviewDataProvider(final IModel<DocumentsFilterSettings> dfsModel,
        final CrawlingDao crawlingDao, final SessionFactory sessionFactory) {
    this.dfsModel = dfsModel;
    this.crawlingDao = crawlingDao;
    this.sessionFactory = sessionFactory;

    // Loads number of crawlings only once per request
    numberOfCrawlingsModel = new LoadableDetachableModel<Long>() {
        private static final long serialVersionUID = 1L;

        @Override/*from   ww w.j a va2s.  c  o  m*/
        public Long load() {
            final Long numberOfCrawlings = crawlingDao.count(sessionFactory.getCurrentSession(),
                    dfsModel.getObject().getProjectId());
            return numberOfCrawlings;
        }
    };
}

From source file:com.romeikat.datamessie.core.view.ui.dataprovider.DocumentsOverviewDataProvider.java

License:Open Source License

public DocumentsOverviewDataProvider(final IModel<DocumentsFilterSettings> dfsModel,
        final DocumentDao documentDao, final SessionFactory sessionFactory) {
    this.dfsModel = dfsModel;
    this.documentDao = documentDao;
    this.sessionFactory = sessionFactory;

    // Loads number of documents only once per request
    numberOfDocumentsModel = new LoadableDetachableModel<Long>() {
        private static final long serialVersionUID = 1L;

        @Override/* w w w  .ja  va 2  s . com*/
        public Long load() {
            final Long numberOfDocuments = documentDao.count(sessionFactory.getCurrentSession(),
                    dfsModel.getObject());
            return numberOfDocuments;
        }
    };
}

From source file:com.romeikat.datamessie.core.view.ui.dataprovider.SourcesOverviewDataProvider.java

License:Open Source License

public SourcesOverviewDataProvider(final IModel<DocumentsFilterSettings> dfsModel, final SourceDao sourceDao,
        final SessionFactory sessionFactory) {
    this.dfsModel = dfsModel;
    this.sourceDao = sourceDao;
    this.sessionFactory = sessionFactory;

    // Loads number of documents only once per request
    numberOfSourcesModel = new LoadableDetachableModel<Long>() {
        private static final long serialVersionUID = 1L;

        @Override//from  ww  w  .j  a  va2 s.  c  om
        public Long load() {
            final Long userId = DataMessieSession.get().getUserId();
            final Long numberOfDocuments = sourceDao.count(sessionFactory.getCurrentSession(), userId,
                    dfsModel.getObject().getProjectId());
            return numberOfDocuments;
        }
    };
}

From source file:com.thoughtworks.go.server.dao.PipelineSqlMapDaoCachingTest.java

License:Apache License

@Before
public void setup() throws Exception {
    transactionSynchronizationManager = mock(TransactionSynchronizationManager.class);
    goCache = new StubGoCache(new TestTransactionSynchronizationManager());
    goCache.clear();/*from  w w w. ja  va  2  s .c  o  m*/
    mockTemplate = mock(SqlMapClientTemplate.class);
    repository = mock(MaterialRepository.class);
    environmentVariableDao = mock(EnvironmentVariableDao.class);
    SessionFactory mockSessionFactory = mock(SessionFactory.class);
    repository = mock(MaterialRepository.class);
    transactionTemplate = mock(TransactionTemplate.class);
    GoConfigDao configFileDao = mock(GoConfigDao.class);
    timeProvider = mock(TimeProvider.class);
    pipelineDao = new PipelineSqlMapDao(null, repository, goCache, environmentVariableDao, transactionTemplate,
            null, transactionSynchronizationManager, null, configFileDao, mock(Database.class),
            mockSessionFactory, timeProvider);
    pipelineDao.setSqlMapClientTemplate(mockTemplate);
    Session session = mock(Session.class);
    when(mockSessionFactory.getCurrentSession()).thenReturn(session);
    when(configFileDao.load()).thenReturn(GoConfigMother.defaultCruiseConfig());
}

From source file:com.trifork.stamdata.persistence.PersistenceModule.java

License:Mozilla Public License

@Provides
protected Session provideSession(SessionFactory factory) {
    return factory.getCurrentSession();
}

From source file:com.ts.ch.gui.CertifiedHelperController.java

private void doSearch() {
    try {//from  w w w  .  j  ava 2s  .  c o m
        CertifiedHelperModel model = (CertifiedHelperModel) getModel();
        String trackingNumber = model.getTrackingNumber();

        SessionFactory sf = database.buildSesionFactory();
        Session session = sf.getCurrentSession();
        session.beginTransaction();

        GeneralDAOImpl dao = new GeneralDAOImpl();
        dao.setSessionFactory(sf);

        Search search = new Search(Package.class);
        search.addFilterEqual("trackingNumber", trackingNumber);
        Package p = (Package) dao.searchUnique(search);

        model.setAddressFrom(p.getShipment().getCustomer().getMailingAddress());
        model.setAddressTo(p.getShipment().getShipToMailingAddress());

        session.getTransaction().commit();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}