Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.codejava.hibernate;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.boot.MetadataSources;
- import org.hibernate.boot.registry.StandardServiceRegistry;
- import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
- /**
- * BookManager.java
- * A Hibernate hello world program
- * @author www.codejava.net
- *
- */
- public class BookManager {
- protected SessionFactory sessionFactory;
- protected void setup() {
- /*final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
- .configure() // configures settings from hibernate.cfg.xml
- .build();*/
- final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
- .configure("hibernate.cfg.xml") // configures settings from hibernate.cfg.xml
- .build();
- try {
- sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
- } catch (Exception ex) {
- StandardServiceRegistryBuilder.destroy(registry);
- throw new RuntimeException(ex);
- }
- }
- protected void exit() {
- sessionFactory.close();
- }
- protected void create() {
- Book book = new Book();
- /*book.setTitle("Effective Java");
- book.setAuthor("Joshua Bloch");
- book.setPrice(32.59f);*/
- book.setTitle("Effective Python");
- book.setAuthor("Mahesh Iyer");
- book.setPrice(32.69f);
- Session session = sessionFactory.openSession();
- session.beginTransaction();
- session.save(book);
- session.getTransaction().commit();
- session.close();
- }
- protected void read() {
- Session session = sessionFactory.openSession();
- long bookId = 20;
- Book book = session.get(Book.class, bookId);
- System.out.println("Title: " + book.getTitle());
- System.out.println("Author: " + book.getAuthor());
- System.out.println("Price: " + book.getPrice());
- session.close();
- }
- protected void update() {
- Book book = new Book();
- book.setId(20);
- book.setTitle("Ultimate Java Programming");
- book.setAuthor("Nam Ha Minh");
- book.setPrice(19.99f);
- Session session = sessionFactory.openSession();
- session.beginTransaction();
- session.update(book);
- session.getTransaction().commit();
- session.close();
- }
- protected void delete() {
- Book book = new Book();
- book.setId(20);
- Session session = sessionFactory.openSession();
- session.beginTransaction();
- session.delete(book);
- session.getTransaction().commit();
- session.close();
- }
- public static void main(String[] args) {
- BookManager manager = new BookManager();
- manager.setup();
- manager.create();
- //manager.delete();
- manager.exit();
- }
- }
Add Comment
Please, Sign In to add comment