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:bikerent.AddDeleteEntity.java

private static void addAccessiores(Session session) {

    // HELMET 1 ################
    Helmet h1 = new Helmet(5.0);

    // HELMET 2 ################
    Helmet h2 = new Helmet(7.0);

    // HELMET 3 ################
    Helmet h3 = new Helmet(4.0);

    // LOCK 1 ################
    Chainlock c1 = new Chainlock(5.0, 2, CableLock);

    // LOCK 2 ################
    Chainlock c2 = new Chainlock(6.0, 3, ChainLock);

    // LOCK 3 ################
    Chainlock c3 = new Chainlock(8.0, 7, ULock);

    // TRAILER 1 ################
    Trailer t1 = new Trailer(15.0, ChildrenTailor, false);

    // TRAILER 2 ################
    Trailer t2 = new Trailer(20.0, GoodsTailer, true);

    session.save(h1);
    session.save(h2);/*from w w w.  jav  a  2 s.  c o m*/
    session.save(h3);
    session.save(c1);
    session.save(c2);
    session.save(c3);
    session.save(t1);
    session.save(t2);
}

From source file:bikerent.BikeRent.java

public static void main(String[] args) {
    // Configure the session factory
    configureSessionFactory();/*w  w  w.  j a va  2 s.  co  m*/

    Session session = null;
    Transaction tx = null;

    try {
        session = sessionFactory.openSession();
        tx = session.beginTransaction();

        // Creating Contact entity that will be save to the sqlite database
        Address myAddress = new Address();

        // Saving to the database
        session.save(myAddress);

        // Committing the change in the database.
        session.flush();
        tx.commit();

        // Fetching saved data
        List<Address> userList = session.createQuery("from Address").list();

        for (Address add : userList) {
            System.out.println("Id: " + add.getId());
        }

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

        // Rolling back the changes to make the data consistent in case of any failure 
        // in between multiple database write operations.
        tx.rollback();
    } finally {
        if (session != null) {
            session.close();
        }
    }

}

From source file:bikerent.Test1.java

private static void addCustomer(Session session) {

    // CUSTOMER 1 #########################

    Date d1 = generateDate("01.11.1990");

    Address a1 = new Address("Teststreet", "12", "22456", "Hamburg", "Germany");

    Customer c1 = new Customer("Karo", "Wochnik", d1, // 01.11.1990
            "k.wochnik@bla.de", "12345", a1);

    // CUSTOMER 2 #########################

    Date d2 = generateDate("05.02.1990");

    Address a2 = new Address("Blastreet", "4", "22489", "Hamburg", "Germany");

    Customer c2 = new Customer("Torben", "Zurhelle", d2, // 05.02.1990
            "t.zurhelle@test.de", "12345", a2);

    // CUSTOMER 3 #########################

    Date d3 = generateDate("17.05.1984");

    Address a3 = new Address("Britstreet", "67", "55678", "Berlin", "Germany");

    Customer c3 = new Customer("Britney", "Spears", d3, // 17.05.1984
            "britney@spears.de", "12346", a3);

    session.save(c1);
    session.save(c2);/* w ww.j a v a2  s. c o  m*/
    session.save(c3);
}

From source file:bola.dao.UserDao.java

public void addUser(RegistrationForm regform) {
    Transaction trns = null;/*w  ww  .j  ava  2 s . c o  m*/
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        trns = session.beginTransaction();
        session.save(regform);
        session.getTransaction().commit();
    } catch (RuntimeException e) {
        if (trns != null) {
            trns.rollback();
        }
    } finally {
        session.flush();
        session.close();
    }
}

From source file:bookstore.BookStoreManager.java

public void addOneToOne() {
    try {/*from ww  w  .ja v a  2s. c o  m*/
        //Tao session va bat dau lam viec voi session do
        Session session = sf.openSession();
        session.beginTransaction();

        //Tao mot cuon sach ten la lap trinh hava nang cao
        Book book = new Book("Lap Trinh Java nang cao");
        //Tao chi tiet cua cuon sach do vao va dua doi tuong book vua tao vao trong ham khoi tao cua bookdetail
        Bookdetail bookdetail = new Bookdetail(book, 100, "Nha xuat ban tre");

        //Dung phuong thuc save de luu doi tuong book va bookdetail vao 2 bang 
        session.save(book);
        session.save(bookdetail);
        session.getTransaction().commit();
        //Sau khi hoan thanh cong viec thi dong sesson lai
        session.close();
        System.out.println("Done");
    } catch (Exception e) {
    }
}

From source file:bookstore.BookStoreManager.java

public void addManyToMany() {
    try {//from w w  w.  ja va2s. c  o  m
        //Tao session va bat dau lam viec voi session do
        Session session = sf.openSession();
        session.beginTransaction();

        //Tao cac cuon sach
        Book book1 = new Book("Tac Pham A");
        Book book2 = new Book("Tac Pham B");

        //Tao cac tac gia
        Author author1 = new Author("Tac Gia A");
        Author author2 = new Author("Tac Gia B");

        //Luu cac tac gia do vao database
        session.save(author1);
        session.save(author2);

        //tao cac quan he nhu la 1 cuon sach thi co nhieu tac gia va 1 tac gia thi co nhieu cuon sach
        book1.getAuthors().add(author1);
        book1.getAuthors().add(author2);
        book2.getAuthors().add(author1);

        //luu cac cuon sach lai
        session.save(book1);
        session.save(book2);

        session.getTransaction().commit();
        //sau khi hoan thanh thi dong session lai
        session.close();
        System.out.println("Done");
    } catch (Exception e) {
    }
}

From source file:bookstore.BookStoreManager.java

public void createCategory() throws IOException {
    try {//from w w w .  j a v  a2 s.  c o  m
        //Nhap nguoi dung nhap ten cua category ty ban phim
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Input name of new category: ");
        String name = input.readLine();

        //dung bien sf de tao ra 1 session bang phuong thuc openSession
        Session session = sf.openSession();
        //sau khi tao session thi dung phuong thuc beginTransaction() de cho session do bat dau lam viec
        session.beginTransaction();

        //Tao 1 category co ten duoc nhap tu ban phim
        Category category = new Category(name);

        //Dung phuong thuc save() de luu category vua tao vao bang category;
        session.save(category);
        //Sau do ta se commit cau lenh 
        session.getTransaction().commit();
        //Sau khi hoan thanh viec tao 1 category thi ta dong session do lai
        session.close();
        System.out.println("DONE");

    } catch (Exception e) {
    }
}

From source file:bookstore.BookStoreManager.java

public void addOneToMany() {
    try {//from  w ww .j a v a  2s. c o m
        //Tao sesion va bat dau lam viec voi session do
        Session session = sf.openSession();
        session.beginTransaction();

        //Tao 2 cuon sach moi 
        Book book1 = new Book("Truyen Kieu");
        Book book2 = new Book("Luc van tien");

        //Tao mot category moi 
        Category category = new Category("Tho");

        //Luu category do vao database
        session.save(category);

        //Set category co 2 cuon sach do hoac ta co the set list nhun cuon
        //sach vao 1 category qua phuong thuc setBook() cua categoyry
        book1.setCategory(category);
        book2.setCategory(category);

        //luu 2 cuon sach lai
        session.save(book1);
        session.save(book2);
        session.getTransaction().commit();
        //sau khi thuc hien xong thi dong session lai
        session.close();
        System.out.println("DONE");
    } catch (Exception e) {
    }
}

From source file:bookstore.CustomerUtils.java

public static int createCustomer(CustomerDAO customer) {
    int id = -1;/*from   w  w w . j a  v  a 2s .  c om*/

    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = session.beginTransaction();
    id = (Integer) session.save(customer);
    tx.commit();

    return id;

}

From source file:br.al.contractmanager.dao.ArquivoDao.java

@Transactional
public boolean adicionar(Arquivo arquivo) {
    Session session = sessionFactory.getCurrentSession();
    try {/*from  w  ww  .  j  av a 2 s .c  o m*/
        session.save(arquivo);
        return true;
    } catch (HibernateException e) {
        System.out.println(e.getMessage());
    }
    return false;
}