Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.top.shelf.lib.entityManagers;
- import java.util.*;
- import javax.persistence.EntityManager;
- import javax.persistence.EntityManagerFactory;
- import javax.persistence.Persistence;
- import javax.persistence.Query;
- public class BookManager {
- private static final String PERSISTENCE_UNIT_NAME = "Book";
- private static EntityManagerFactory factory;
- private String userName;
- private String password;
- public BookManager() {
- }
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public static void main(String args[]) {
- factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
- EntityManager em = factory.createEntityManager();
- Book books[] = new Book[5];
- em.getTransaction().begin();
- books[0] = new Book.Builder("Panzee, Jim", "My Life In The Jungle",
- "123456ABC").build();
- books[1] = new Book.Builder("Furst, Hugo", "Over the Cliff",
- "123456ABD").build();
- books[2] = new Book.Builder("Salmon, Ella", "The Long Trek up Stream",
- "123456ABE").build();
- em.persist(books[0]);
- em.persist(books[1]);
- em.persist(books[2]);
- System.out.println("Write");
- em.getTransaction().commit();
- Book b = em.find(Book.class, 1);
- //System.out.println(b.getAuthor());
- Query q = em.createQuery("SELECT b FROM Book b");
- // q.setParameter("title", "Over the Cliff");
- try {
- System.out.println("HERE");
- System.out.println(q.getResultList().size());
- for (Book b1 : (List<Book>) q.getResultList()) {
- System.out.println(b1.getTitle());
- }
- System.out.println("DONE");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment