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:cd_modelos_dao.VentasDAO.java

public void eliminarVenta(int id) {
    SessionFactory sf = HibernateUtil.getSessionFactory();
    Transaction t = null;//w  w  w  .  j av  a2s. c  om
    Session s = sf.openSession();
    t = s.beginTransaction();
    Query q = s.createQuery("delete from VentasPlanta where id = :id");
    q.setInteger("id", id);
    q.executeUpdate();
    t.commit();
    s.close();
}

From source file:cd_modelos_dao.VentasDAO.java

public void ingresarVenta(String data) {

    VentasPlanta venta = crearVenta(data, false);
    crearArrayJSON(venta, "lineas", data, false);

    SessionFactory sf = null;
    Transaction t = null;/*from  w ww.j a v  a  2 s .c  o  m*/
    Session s = null;
    try {
        sf = HibernateUtil.getSessionFactory();
        s = sf.openSession();
        t = s.beginTransaction();
        s.save(venta);
        t.commit();
        s.close();
        for (PlantasVenta plantasVenta : venta.getPlantasVentas()) {
            plantasVenta.setVentasPlanta(venta);
            try {
                EtapasPlantaDAO eD = new EtapasPlantaDAO();
                eD.actualizarEtapasPlanta(plantasVenta.getEtapasPlanta());
            } catch (Exception e) {
                System.out.println("1");
                System.out.println(e);
            }
            try {
                PlantasVentaDAO pvd = new PlantasVentaDAO();
                plantasVenta.setId(pvd.maxId() + 1);
                pvd.ingresarPlantasVenta(plantasVenta);
            } catch (Exception e) {
                System.out.println("2");
                System.out.println(e);
            }
        }

    } catch (HibernateException e) {
        t.rollback();
        throw new RuntimeException("No se pudo guardar el servicio");
    }
}

From source file:cd_modelos_dao.VentasDAO.java

public VentasPlanta consultarVentaPorId(int id) {
    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session s = sf.openSession();
    VentasPlanta ser = (VentasPlanta) s.get(VentasPlanta.class, id);
    List<PlantasVenta> listado = new LinkedList<>();
    Query q = s.createQuery("from PlantasVenta where id_ventas_planta = " + id);
    try {/*from w  w w . ja v  a  2s.  c  o  m*/
        listado = q.list();

    } catch (Exception e) {
        System.out.println("____________________________________________________");
        System.out.println(e);
        System.out.println("____________________________________________________");
    }
    for (int i = 0; i < listado.size(); i++) {
        Plantas p = (Plantas) s.get(Plantas.class, listado.get(i).getEtapasPlanta().getPlantas().getId());
        Etapas p1 = (Etapas) s.get(Etapas.class, listado.get(i).getEtapasPlanta().getEtapas().getId());
        listado.get(i).setEtapasPlanta(new EtapasPlanta());
        listado.get(i).getEtapasPlanta().setPlantas(p);
        listado.get(i).getEtapasPlanta().setEtapas(p1);
    }

    Set<PlantasVenta> listado2 = new HashSet<>(listado);
    ser.setPlantasVentas(listado2);
    s.close();
    if (ser != null) {
        return ser;
    }
    return null;
}

From source file:cd_modelos_dao.VentasDAO.java

public void actualizarVenta(VentasPlanta v1, VentasPlanta v2) {
    SessionFactory sf = HibernateUtil.getSessionFactory();
    //Listas//from w ww  .  j av  a  2 s  . c  om
    List<PlantasVenta> a1 = new LinkedList<>(v1.getPlantasVentas());
    List<PlantasVenta> a2 = new LinkedList<>(v2.getPlantasVentas());
    try {
        Session s = sf.openSession();
        Transaction t = s.beginTransaction();
        s.update(v2);

        System.out.println("la cantidad de lineas de enta es");
        System.out.println(v2.getPlantasVentas().size());
        for (PlantasVenta plantasVenta : v2.getPlantasVentas()) {
            try {
                EtapasPlantaDAO eD = new EtapasPlantaDAO();
                eD.ingresarActualizarEtapasPlanta(plantasVenta.getEtapasPlanta());
            } catch (Exception e) {
                System.out.println("1");
                System.out.println(e);
            }
            try {
                PlantasVentaDAO pvd = new PlantasVentaDAO();
                if (plantasVenta.getId() < 0) {

                    plantasVenta.setId(pvd.maxId() + 1);
                }
                pvd.ingresarPlantasVenta(plantasVenta);
            } catch (Exception e) {
                System.out.println("2");
                System.out.println(e);
            }
        }

        insertarDiferenciasVentasPlanta(calcularDiferenciasVentasPlanta(v1, v2));
        insertarDiferenciasPlantasVenta(calcularDiferenciasPlantasVenta(a1, a2));
        t.commit();
        s.close();
    } catch (HibernateException he) {
        System.out.println("Paso algo 1");

        System.out.println(he);
        he.getMessage();
    }
}

From source file:cd_modelos_dao.VentasDAO.java

public void insertarDiferenciasVentasPlanta(List<ModificacionesVentasPlanta> lista) {
    SessionFactory sf = null;
    Transaction t = null;/*from   www.j  a va2s.co m*/
    Session s = null;
    try {
        sf = HibernateUtil.getSessionFactory();
        s = sf.openSession();
        t = s.beginTransaction();
        for (ModificacionesVentasPlanta elemento : lista) {
            s.save(elemento);
        }
        t.commit();
        s.close();
    } catch (HibernateException e) {
        t.rollback();
        System.out.print("No se pudo guardar la venta planta");
        System.out.println(e);

    }

}

From source file:cd_modelos_dao.VentasDAO.java

public void insertarDiferenciasPlantasVenta(List<ModificacionesPlantaVenta> lista) {
    SessionFactory sf = null;
    Transaction t = null;/*from www. j  av a 2 s  .com*/
    Session s = null;
    try {
        sf = HibernateUtil.getSessionFactory();
        s = sf.openSession();
        t = s.beginTransaction();
        for (ModificacionesPlantaVenta elemento : lista) {
            s.save(elemento);
        }
        t.commit();
        s.close();
    } catch (HibernateException e) {
        t.rollback();
        System.out.println("No se pudo guardar la plnanta venta");
        System.out.println(e);
    }

}

From source file:cd_modelos_dao.VentasDAO.java

public List<VentasPlanta> obtenerVentas() {
    List<VentasPlanta> lista = new LinkedList<>();
    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session s = sf.openSession();
    Query q = s.createQuery("from VentasPlanta");
    lista = q.list();//from www  .  ja  v a 2 s  .c  om
    s.close();

    return lista;
}

From source file:cgt.TesteHibernate.java

/**
 * @param args the command line arguments
 */// ww w.  j  a  va 2 s . c o m
public static void main(String[] args) {
    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session sessao = sf.openSession();

    Pessoa pes = new Pessoa();
    Endereco endereco = new Endereco();

    endereco.setBairro("centro");
    endereco.setCep(29725000);
    endereco.setCidade("Marilndia");
    endereco.setRua("Virginia Paganini Tozzi");

    pes.setCpf("12156544727");
    pes.setNome("Artur");
    pes.setEndereco(endereco);
    pes.setTelefone01(000000);
    pes.setTelefone02(111111);

    Transaction ts = sessao.beginTransaction();

    sessao.save(endereco);
    sessao.save(pes);

    ts.commit();
    sessao.flush();
    sessao.close();
}

From source file:ch.algotrader.cache.CacheTest.java

License:Open Source License

@BeforeClass
public static void beforeClass() {

    context = new AnnotationConfigApplicationContext();
    context.getEnvironment().setActiveProfiles("embeddedDataSource", "simulation");

    // register in-memory db
    EmbeddedDatabaseFactory dbFactory = new EmbeddedDatabaseFactory();
    dbFactory.setDatabaseType(EmbeddedDatabaseType.H2);
    dbFactory.setDatabaseName("testdb;MODE=MYSQL;DATABASE_TO_UPPER=FALSE");

    database = dbFactory.getDatabase();/*w  w  w.jav  a 2 s .co m*/
    context.getDefaultListableBeanFactory().registerSingleton("dataSource", database);

    EngineManager engineManager = Mockito.mock(EngineManager.class);
    Mockito.when(engineManager.getCurrentEPTime()).thenReturn(new Date());
    Mockito.when(engineManager.getCurrentEPTime()).thenReturn(new Date());
    context.getDefaultListableBeanFactory().registerSingleton("engineManager", engineManager);

    AtomicReference<TransactionService> transactionService = new AtomicReference<>();
    Engine engine = new NoopEngine(StrategyImpl.SERVER);

    context.getDefaultListableBeanFactory().registerSingleton("serverEngine", engine);

    ExternalMarketDataService externalMarketDataService = Mockito.mock(ExternalMarketDataService.class);
    context.getDefaultListableBeanFactory().registerSingleton("externalMarketDataService",
            externalMarketDataService);

    context.getDefaultListableBeanFactory().registerSingleton("historicalDataService",
            new NoopHistoricalDataServiceImpl());

    Mockito.when(externalMarketDataService.getFeedType()).thenReturn(FeedType.IB.name());

    // register Wirings
    context.register(CommonConfigWiring.class, CoreConfigWiring.class, EventDispatchWiring.class,
            EventDispatchPostInitWiring.class, HibernateWiring.class, CacheWiring.class, DaoWiring.class,
            ServiceWiring.class, SimulationWiring.class);

    context.refresh();

    transactionService.set(context.getBean(TransactionService.class));

    cache = context.getBean(CacheManager.class);
    txTemplate = context.getBean(TransactionTemplate.class);

    // create the database
    ResourceDatabasePopulator dbPopulator = new ResourceDatabasePopulator();
    dbPopulator.addScript(new ClassPathResource("/db/h2/h2.sql"));
    DatabasePopulatorUtils.execute(dbPopulator, database);

    // populate the database
    SessionFactory sessionFactory = context.getBean(SessionFactory.class);
    Session session = sessionFactory.openSession();

    Exchange exchange1 = new ExchangeImpl();
    exchange1.setName("IDEALPRO");
    exchange1.setCode("IDEALPRO");
    exchange1.setTimeZone("US/Eastern");
    session.save(exchange1);

    SecurityFamily family1 = new SecurityFamilyImpl();
    family1.setName("FX");
    family1.setTickSizePattern("0<0.1");
    family1.setCurrency(Currency.USD);
    family1.setExchange(exchange1);
    family1.setTradeable(true);
    family1.setContractSize(1.0);
    securityFamilyId1 = (Long) session.save(family1);

    SecurityFamily family2 = new SecurityFamilyImpl();
    family2.setName("NON_TRADEABLE");
    family2.setTickSizePattern("0<0.1");
    family2.setCurrency(Currency.USD);
    family2.setTradeable(false);
    family2.setContractSize(1.0);
    securityFamilyId2 = (Long) session.save(family2);

    Forex security1 = new ForexImpl();
    security1.setSymbol("EUR.USD");
    security1.setBaseCurrency(Currency.EUR);
    security1.setSecurityFamily(family1);
    securityId1 = (Long) session.save(security1);

    Forex security2 = new ForexImpl();
    security2.setSymbol("GBP.USD");
    security2.setBaseCurrency(Currency.GBP);
    security2.setSecurityFamily(family1);
    security2.setUnderlying(security1);
    securityId2 = (Long) session.save(security2);

    Strategy strategy1 = new StrategyImpl();
    strategy1.setName(STRATEGY_NAME);
    strategyId1 = (Long) session.save(strategy1);

    Position position1 = new PositionImpl();
    position1.setQuantity(222);
    position1.setStrategy(strategy1);
    position1.setSecurity(security2);
    position1.setCost(new BigDecimal(0.0));
    position1.setRealizedPL(new BigDecimal(0.0));

    session.save(position1);

    Property property1 = new PropertyImpl();
    property1.setName(PROPERTY_NAME);
    property1.setDoubleValue(10.0);
    property1.setPropertyHolder(strategy1);
    session.save(property1);
    strategy1.getProps().put(PROPERTY_NAME, property1);

    Account account1 = new AccountImpl();
    account1.setName("TEST");
    account1.setBroker("TEST");
    account1.setOrderServiceType(OrderServiceType.SIMULATION.toString());
    accountId1 = (Long) session.save(account1);

    session.flush();
    session.close();
}

From source file:ch.algotrader.dao.AbstractDaoTest.java

License:Open Source License

@Before
public void setup() throws Exception {

    SessionFactory sessionFactory = DATABASE.getSessionFactory();
    this.session = sessionFactory.openSession();

    TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(this.session));

    this.dao = new GenericItemDao(sessionFactory);
}