Guest User

BookManager.java

a guest
Sep 18th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.62 KB | None | 0 0
  1. package net.codejava.hibernate;
  2.  
  3. import org.hibernate.Session;
  4. import org.hibernate.SessionFactory;
  5. import org.hibernate.boot.MetadataSources;
  6. import org.hibernate.boot.registry.StandardServiceRegistry;
  7. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  8.  
  9. /**
  10.  * BookManager.java
  11.  * A Hibernate hello world program
  12.  * @author www.codejava.net
  13.  *
  14.  */
  15. public class BookManager {
  16.     protected SessionFactory sessionFactory;
  17.  
  18.     protected void setup() {
  19.         /*final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
  20.                 .configure() // configures settings from hibernate.cfg.xml
  21.                 .build();*/
  22.         final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
  23.                 .configure("hibernate.cfg.xml") // configures settings from hibernate.cfg.xml
  24.                 .build();
  25.         try {
  26.             sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
  27.         } catch (Exception ex) {
  28.             StandardServiceRegistryBuilder.destroy(registry);
  29.             throw new RuntimeException(ex);
  30.         }
  31.     }
  32.  
  33.     protected void exit() {
  34.         sessionFactory.close();
  35.     }
  36.  
  37.     protected void create() {
  38.         Book book = new Book();
  39.         /*book.setTitle("Effective Java");
  40.         book.setAuthor("Joshua Bloch");
  41.         book.setPrice(32.59f);*/
  42.  
  43.         book.setTitle("Effective Python");
  44.         book.setAuthor("Mahesh Iyer");
  45.         book.setPrice(32.69f);
  46.  
  47.         Session session = sessionFactory.openSession();
  48.         session.beginTransaction();
  49.  
  50.         session.save(book);
  51.  
  52.         session.getTransaction().commit();
  53.         session.close();
  54.     }
  55.  
  56.     protected void read() {
  57.         Session session = sessionFactory.openSession();
  58.  
  59.         long bookId = 20;
  60.         Book book = session.get(Book.class, bookId);
  61.  
  62.         System.out.println("Title: " + book.getTitle());
  63.         System.out.println("Author: " + book.getAuthor());
  64.         System.out.println("Price: " + book.getPrice());
  65.  
  66.         session.close();
  67.     }
  68.  
  69.     protected void update() {
  70.         Book book = new Book();
  71.         book.setId(20);
  72.         book.setTitle("Ultimate Java Programming");
  73.         book.setAuthor("Nam Ha Minh");
  74.         book.setPrice(19.99f);
  75.  
  76.         Session session = sessionFactory.openSession();
  77.         session.beginTransaction();
  78.  
  79.         session.update(book);
  80.  
  81.         session.getTransaction().commit();
  82.         session.close();
  83.     }
  84.  
  85.     protected void delete() {
  86.         Book book = new Book();
  87.         book.setId(20);
  88.  
  89.         Session session = sessionFactory.openSession();
  90.         session.beginTransaction();
  91.  
  92.         session.delete(book);
  93.  
  94.         session.getTransaction().commit();
  95.         session.close();
  96.     }
  97.    
  98.     public static void main(String[] args) {
  99.         BookManager manager = new BookManager();
  100.         manager.setup();
  101.         manager.create();
  102.         //manager.delete();
  103.        
  104.         manager.exit();
  105.     }
  106.  
  107. }
Add Comment
Please, Sign In to add comment