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:bazydanych.OneToManyTest.java

private Integer addEmployee(String fname, String lname, int salary, Set cert) {
    Session session = factory.openSession();
    Transaction tx = null;/*from  w  w w. j  av a2 s.c o  m*/
    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();
        fail();
    } finally {
        session.close();
    }
    return employeeID;
}

From source file:bd.ac.seu.contactapp.controller.ContactController.java

public void addContact(ActionEvent event) {
    System.out.println("Saving entry");
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();//from  www .  ja v  a2s .  c o  m
    session.save(contact);
    session.getTransaction().commit();
    session.close();
    contact = new Contact();
}

From source file:bd.ac.seu.hibernate.FXMLDocumentController.java

@FXML
private void handleStudentSaveAction(ActionEvent event) {
    String id = idField.getText();
    String name = nameField.getText();
    Date dob = Date.from(dobPicker.getValue().atStartOfDay(ZoneId.systemDefault()).toInstant());
    String house = houseField.getText();
    String street = streetField.getText();
    String city = cityField.getText();
    Student student = new Student(id, name, dob, new Address(house, street, city));

    Session session = null;
    Transaction transaction = null;/* w  w w  .  j  av a  2s  . c  o m*/

    try {
        session = SessionFactorySingleton.getSessionFactory().openSession();
        transaction = session.beginTransaction();
        session.save(student);
        transaction.commit();
        students.add(student);
    } catch (Exception e) {
        if (transaction != null)
            transaction.rollback();
    } finally {
        session.close();
    }
}

From source file:bd.ac.seu.hibernate.FXMLDocumentController.java

@FXML
private void handleCourseSaveAction(ActionEvent event) {
    String code = codeField.getText();
    String title = titleField.getText();
    double credits = Double.parseDouble(creditsField.getText());

    Course course = new Course(code, title, credits);

    Session session = null;
    Transaction transaction = null;//from ww  w  .j ava 2 s.  co m

    try {
        session = SessionFactorySingleton.getSessionFactory().openSession();
        transaction = session.beginTransaction();
        session.save(course);
        transaction.commit();
        courses.add(course);
    } catch (Exception e) {
        if (transaction != null)
            transaction.rollback();
    } finally {
        session.close();
    }
}

From source file:bd.ac.seu.labexam.AdministratorCreator.java

public void create() {
    Administrator administrator = new Administrator(userId, Md5Generator.generateMd5(password));

    Session session = SessionFactorySingleton.getSessionFactory().openSession();
    String response = "";
    FacesMessage message;/*from w w w  . j a va 2s  .  c  o m*/
    try {
        session.beginTransaction();
        session.save(administrator);
        session.getTransaction().commit();
        response = "Successfully added administrator " + userId;
        message = new FacesMessage(FacesMessage.SEVERITY_INFO, response, null);
    } catch (Exception e) {
        e.printStackTrace();
        response = "Failed to add administrator " + userId;
        message = new FacesMessage(FacesMessage.SEVERITY_ERROR, response, null);
    }

    FacesContext.getCurrentInstance().addMessage(null, message);
}

From source file:bd.ac.seu.pos.AddSupplierController.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//  w w w  . j a  v  a  2s  .  c om
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {
        Session session = SessionFactorySingleton.getSessionFactory().openSession();

        int id = Integer.parseInt(request.getParameter("supplierId"));
        String name = request.getParameter("supplierName");
        String address = request.getParameter("supplierAddress");

        Supplier supplier = new Supplier(id, name, address);
        session.beginTransaction();
        session.save(supplier);
        session.getTransaction().commit();

        List<Supplier> suppliers = session.createCriteria(Supplier.class).list();

        request.setAttribute("supplier", supplier);
        request.setAttribute("suppliers", suppliers);

        getServletContext().getRequestDispatcher("/supplierView.jsp").forward(request, response);
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:bd.ac.seu.simplewebapp.model.Student.java

public void saveStudent(ActionEvent event) {
    System.out.println("We are calling the save method");

    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = session.beginTransaction();
    Student student = new Student(this.studentId, this.studentName);
    session.save(student);
    transaction.commit();/*from   w  ww  .j  a  v  a  2 s.com*/
    session.close();
}

From source file:be.facet.it.dojo.customershibernate.repository.AddressRepository.java

@Override
public void save(Address entity) {
    Session session = this.startTransaction();
    session.save(entity);
    this.stopTransaction(session);
}

From source file:be.facet.it.dojo.customershibernate.repository.CustomerRepository.java

@Override
public void save(Customer entity) {
    Session session = this.startTransaction();
    session.save(entity);
    this.stopTransaction(session);
}

From source file:be.facet.it.dojo.customershibernate.repository.ProductRepository.java

@Override
public void save(Product entity) {
    Session session = this.startTransaction();
    session.save(entity);
    this.stopTransaction(session);
}