Guest User

Untitled

a guest
Jan 15th, 2016
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.85 KB | None | 0 0
  1. package com.top.shelf.lib.entityManagers;
  2.  
  3. import java.util.*;
  4.  
  5. import javax.persistence.EntityManager;
  6. import javax.persistence.EntityManagerFactory;
  7. import javax.persistence.Persistence;
  8. import javax.persistence.Query;
  9.  
  10. public class BookManager {
  11.  
  12.     private static final String PERSISTENCE_UNIT_NAME = "Book";
  13.     private static EntityManagerFactory factory;
  14.  
  15.     private String userName;
  16.     private String password;
  17.  
  18.     public BookManager() {
  19.     }
  20.  
  21.     public String getUserName() {
  22.         return userName;
  23.     }
  24.  
  25.     public void setUserName(String userName) {
  26.         this.userName = userName;
  27.     }
  28.  
  29.     public String getPassword() {
  30.         return password;
  31.     }
  32.  
  33.     public void setPassword(String password) {
  34.         this.password = password;
  35.     }
  36.  
  37.     public static void main(String args[]) {
  38.         factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
  39.         EntityManager em = factory.createEntityManager();
  40.  
  41.         Book books[] = new Book[5];
  42.  
  43.         em.getTransaction().begin();
  44.  
  45.         books[0] = new Book.Builder("Panzee, Jim", "My Life In The Jungle",
  46.                 "123456ABC").build();
  47.         books[1] = new Book.Builder("Furst, Hugo", "Over the Cliff",
  48.                 "123456ABD").build();
  49.         books[2] = new Book.Builder("Salmon, Ella", "The Long Trek up Stream",
  50.                 "123456ABE").build();
  51.  
  52.         em.persist(books[0]);
  53.         em.persist(books[1]);
  54.         em.persist(books[2]);
  55.  
  56.         System.out.println("Write");
  57.        
  58.         em.getTransaction().commit();
  59.  
  60.         Book b = em.find(Book.class, 1);
  61.         //System.out.println(b.getAuthor());
  62.  
  63.         Query q = em.createQuery("SELECT b FROM Book b");
  64.         // q.setParameter("title", "Over the Cliff");
  65.  
  66.         try {
  67.  
  68.             System.out.println("HERE");
  69.  
  70.             System.out.println(q.getResultList().size());
  71.  
  72.             for (Book b1 : (List<Book>) q.getResultList()) {
  73.                 System.out.println(b1.getTitle());
  74.             }
  75.  
  76.             System.out.println("DONE");
  77.  
  78.         } catch (Exception e) {
  79.             e.printStackTrace();
  80.         }
  81.     }
  82. }
Add Comment
Please, Sign In to add comment