Example usage for org.hibernate Session save

List of usage examples for org.hibernate Session save

Introduction

In this page you can find the example usage for org.hibernate Session save.

Prototype

Serializable save(Object object);

Source Link

Document

Persist the given transient instance, first assigning a generated identifier.

Usage

From source file:banco.UsuarioDAO.java

public void addUsuario(Usuario usuario) {

    Transaction trns = null;/*w  w w .  jav a 2s . co m*/
    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session = sf.openSession();

    try {
        trns = session.beginTransaction();
        session.save(usuario);
        session.getTransaction().commit();
    } catch (RuntimeException e) {
        if (trns != null) {
            trns.rollback();
        }
        e.printStackTrace();
    } finally {
        session.flush();
        session.close();
    }

}

From source file:bank.DAO.BillDAO.java

public void addBill(int client_id) {
    //Session session = HibernateUtil.getSessionFactory().openSession();
    /////////////////////
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();

    session.beginTransaction();//from  ww w  .j  a  v a 2  s.co  m

    Bill r = new Bill();

    /*Card card = new Card();
    System.out.println("ClientsDAO: new client");
    card.setNumber(1);
    card.setStart_date(Date.valueOf("2015-04-28"));*/

    /*List<Clients> result = session.createQuery("from Clients clients order by clients.id desc").list();
    session.getTransaction().commit();
    System.out.println(result.get(0));
    r.setId(result.get(0).getId()+1);*/

    //r.setId(1);
    r.setClient_id(client_id);

    // last r.getCard().add(card);

    session.save(r);
    session.getTransaction().commit();
    //session.close();
}

From source file:bank.DAO.CardDAO.java

public void addCard(int bill_id, Date date, Clients clients) {

    //Session session = HibernateUtil.getSessionFactory().openSession();
    /////////////////////
    Bill bill;/*from  w w  w.  j  a  v a 2s.  c  o  m*/

    /*try {
    bill = (Bill) session.createCriteria(Bill.class)
            .add(Restrictions.eq("id", bill_id))
            .uniqueResult();
            
    session.beginTransaction();*/

    //List<Card> result = session.createQuery("from Card card order by card.number desc").list();
    //session.getTransaction().commit();
    /*System.out.println(result.get(0));
    r.setNumber(result.get(0).getNumber()+1);*/
    //r.setNumber(1);

    /*session.getTransaction().commit();
    } finally {
    session.close();
    }*/

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    bill = (Bill) session.createCriteria(Bill.class).add(Restrictions.eq("id", bill_id)).uniqueResult();
    Card r = new Card(clients);
    r.setClient_id(bill.getClient_id());
    r.setBill_id(bill.getId());
    r.setStart_date(date);
    System.out.println(r.getNumber());
    session.save(r);
    session.getTransaction().commit();
}

From source file:bank.DAO.ClientsDAO.java

public void addClients(String passport_No) {
    //Session session = HibernateUtil.getSessionFactory().openSession();
    /////////////ww  w .j  av  a2s .c  o  m
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();

    session.beginTransaction();

    Clients r = new Clients();

    /*Card card = new Card();
    System.out.println("ClientsDAO: new client");
    card.setNumber(1);
    card.setStart_date(Date.valueOf("2015-04-28"));*/

    /*List<Clients> result = session.createQuery("from Clients clients order by clients.id desc").list();
    session.getTransaction().commit();
    System.out.println(result.get(0));
    r.setId(result.get(0).getId()+1);*/

    //r.setId(1);
    r.setPassport_No(passport_No);

    // last r.getCard().add(card);

    session.save(r);
    session.getTransaction().commit();
    //session.close();
}

From source file:basedistribuida.connection.HibernateSession.java

protected int insert(Object clazz) {
    Session session = sessionFactory.openSession();
    int id = -1;//from ww w  . j a va  2s .com
    try {
        session.beginTransaction();
        id = (int) session.save(clazz);
        session.getTransaction().commit();
    } finally {
        session.close();
    }
    return id;
}

From source file:batch.processing.usage.chunk.hibernate.MyFileWriter.java

@Override
public void writeItems(List<Object> items) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//from   w w  w . jav a 2  s . co  m
    System.out.println("Abrindo a transao");
    try {
        LOG.info("Writer chamado");
        for (Object operacao : items) {
            Operation op = (Operation) operacao;
            op.setCalculoProvisao(op.calcularProvisao());
            LOG.info(((Operation) operacao).toString());
            session.save(operacao);
        }
        session.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
        session.getTransaction().rollback();
    }
    System.out.println("Fechando a transao");
}

From source file:batch.processing.usage.tasklet.hibernate.HibernateTasklet.java

@Override
public void process() throws Exception {
    List<Operation> operacoes = new ArrayList<>();
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    InputStream source = this.getClass()
            .getResourceAsStream(this.context.getParameters().get("nomeArquivo").toString());
    try (Scanner scan = new Scanner(source)) {
        while (scan.hasNext()) {
            String[] dados = scan.next().split(";");
            Operation operacao = new Operation(dados[0], new BigDecimal(dados[1]), dados[2],
                    new BigDecimal(dados[3]));
            operacoes.add(operacao);/*from  ww w  . java2s. co  m*/
            Random random = new Random();
            random.nextInt();
            operacao.setId(random.nextInt());
        }

        // calcula o percentual de provisionamento
        for (Operation operacao : operacoes) {
            session.save(operacao);
        }
    }
}

From source file:bazydanych.CRUDTest.java

private int createCar(String marka, int cena) {
    Session session = factory.openSession();
    Transaction tx = null;// w  ww .  j  a  v a 2s  .  co  m
    Integer id = null;
    try {
        tx = session.beginTransaction();
        Samochod car = new Samochod(marka, cena);
        id = (Integer) session.save(car);
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
        fail();
    } finally {
        session.close();
    }
    return id;
}

From source file:bazydanych.KsiazkiTest.java

private Integer addBook(String title, Set heroes) {
    Session session = factory.openSession();
    Transaction tx = null;/*from  www.  j  a v  a 2 s .c om*/
    Integer bookId = null;
    try {
        tx = session.beginTransaction();
        Ksiazka book = new Ksiazka(title);
        book.setBohaterowie(heroes);
        bookId = (Integer) session.save(book);
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
        fail();
    } finally {
        session.close();
    }
    return bookId;
}

From source file:bazydanych.ManageEmployee.java

public Integer addEmployee(String fname, String lname, int salary, Set cert) {
    Session session = factory.openSession();
    Transaction tx = null;/* ww  w . ja v a  2s  .  c  om*/
    Integer employeeID = null;
    try {
        tx = session.beginTransaction();
        Employee employee = new Employee(fname, lname, salary);
        employee.setCertificates(cert);
        employeeID = (Integer) session.save(employee);
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null)
            tx.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }
    return employeeID;
}